绘制Pandas DataFrame中出现的次数 [英] Plot number of occurrences from Pandas DataFrame

查看:333
本文介绍了绘制Pandas DataFrame中出现的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两列的DataFrame.其中一个包含时间戳,另一个包含-某些操作的ID.像这样的东西:

I have a DataFrame with two columns. One of them is containing timestamps and another one - id of some 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

我想知道在一天中执行了多少种特定类型的动作. IE.对于每一天,我需要计算actionX的出现次数,并针对每个日期在X轴上的日期和Y轴上的actionX的发生次数来绘制此数据.

I would like to know how many actions of certain type have been performed in a certain day. I.e. for every day, I need to count the number of occurrences of actionX and plot this data with date on X axis and number of occurrences of actionX on Y axes, for each date.

当然,仅通过遍历数据集,我就可以天真地计算每天的操作.但是,处理pandas/matplotlib的正确方法"是什么?

Of course I can count actions for each day naively just by iterating through my dataset. But what's the "right way" to do in with pandas/matplotlib?

推荐答案

                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

你可以做

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)

但是也许有一种更直接的方法.无论如何以上都应该起作用.

but perhaps there's an even more straightforward way. the above should work anyway.

如果您想将count用作DataFrame,则可以将其转换回

If you want to use counts as a DataFrame, I'd then transform it back

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

这篇关于绘制Pandas DataFrame中出现的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆