'Plotting the frequency of occurrences per date

I'm new to pandas and plotly. And I have a large csv file with two columns, a date column and a column that contains a string of text (event). Each event is a new row.

I need to plot the date on the x-axis, and the y-axis needs to contain how many times a single event occurs on each date.

The file looks like this (just for illustration):

   | Date | Event |
   | -------- | -------------- |
   | 1/1/1017 | event1         |
   | 1/1/1017 | event1         |
   | 1/1/1017 | event2         |
   | 1/1/1017 | event2         |
   | 1/1/1017 | event3         |
   | 6/8/1018 | event1         |
   | 6/8/1018 | event3         |
   | 7/8/1018 | event2         |
   | 7/8/1018 | event2         |

I've been able to count the number of occurences of each event for every date:

import numpy as np
import pandas as pd
import matplotlib
import cufflinks as cf
import plotly
import plotly.offline as py
import plotly.graph_objs as go
import matplotlib.pyplot as plt

cf.go_offline()
py.init_notebook_mode()

df = pd.read_csv('file.csv')

df.Date = pd.to_datetime(df.Date)
df.Date.sort_values().index
df = df.iloc[df.Date.sort_values().index]

data = df.groupby(["Date","Event"]).size()

All that is left is to plot this dataframe, but I want to plot it just for one event, for example 'event1'. So I want the plot to illustrate how many times 'event1' occured each date. I've tried plotting data itself,

data = df.groupby(["Date","Event"]).size()
data.iplot(kind='bar', xTitle='Year', yTitle='Count', title='Events Per Day')

but since this is a large dataframe, it comes out very messy, so I want to do it just one one event, and see their occurences over the course of time.



Solution 1:[1]

If you'd really only like to view one specified event at a time, this is all just a matter of subsetting and grouping your dataset. I would use px.bar() instead of iplot() though. Here's a way to do it:

Plot

enter image description here

Complete code:

import pandas as pd
import numpy as np
import plotly.express as px

nrows = 25

df = pd.DataFrame({'date': np.random.choice(['01.01.2022', '02.01.2022', '03.01.2022'], size = nrows),
                   'event':  np.random.choice(['event1', 'event2', 'event3'], size = nrows)})
df.date = pd.to_datetime(df.date)
df = df.groupby(['date', 'event']).size().reset_index()
df = df.sort_values(['date', 'event'])
df.rename({0:'count'}, axis = 'columns', inplace = True)

fig = px.bar(df[df['event']=='event1'], x='date', y = 'count')
fig.update_layout(title = 'Event 1')
fig.show()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vestland