Number of occurrences from Pandas DataFrame

I have a DataFrame with two columns. One of them contains timestamps, and the other is the identifier of an action. Something like that:

2000-12-29 00:10:00     action1
2000-12-29 00:20:00     action2
2000-12-29 00:30:00     action2
2000-12-29 00:40:00     action1
2000-12-29 00:50:00     action1
...
2000-12-31 00:10:00     action1
2000-12-31 00:20:00     action2
2000-12-31 00:30:00     action2

I would like to know how many actions of a certain type were completed in a given day. That is, for each day, I need to calculate the number of occurrences of actionX and build this data with a date on the X axis and the number of occurrences of actionX on the Y axes for each date.

Of course, I can count actions on every day naively, just repeating my data. But what is the “right way” to do with pandas / matplotlib?

+4
source share
2 answers

Beginning with

                mydate col_name
0  2000-12-29 00:10:00  action1
1  2000-12-29 00:20:00  action2
2  2000-12-29 00:30:00  action2
3  2000-12-29 00:40:00  action1
4  2000-12-29 00:50:00  action1
5  2000-12-31 00:10:00  action1
6  2000-12-31 00:20:00  action2
7  2000-12-31 00:30:00  action2

You can do

df['mydate'] = pd.to_datetime(df['mydate'])
df = df.set_index('mydate')
df['day'] = df.index.date
counts = df.groupby(['day', 'col_name']).agg(len)

, , . .

counts DataFrame,

counts = pd.DataFrame(counts, columns=['count'])
+4

,

df.groupby([df.index.date, 'action']).count()

df.groupby([df.index.date, 'action']).count().plot(kind='bar')

count, . , datetimeindex, @mkln .

+13

Source: https://habr.com/ru/post/1523513/


All Articles