[英]groupby minute, hour, day, month, and year?
I'm searching a solution for my problem, but I can't find. 我正在寻找问题的解决方案,但找不到。 So I'm hoping you can help me.
所以我希望你能帮助我。
Problem: 问题:
I want to count the number of tweets in a minute. 我想计算一分钟内的推文数量。
Dataset: 数据集:
time sentiment
0 2018-05-02 14:28:59.281 0.8104
1 2018-05-02 14:29:03.540 0.6536
2 2018-05-02 14:29:09.570 0.0000
3 2018-05-02 14:29:10.445 0.0000
4 2018-05-02 14:29:22.245 0.0000
5 2018-05-02 14:29:23.349 0.4389
6 2018-05-02 14:29:28.329 0.2732
7 2018-05-02 14:29:30.410 0.2732
8 2018-05-02 14:29:46.122 0.0000
9 2018-05-02 14:29:53.194 -0.5562
I want to count the numbers of tweets in a minute and then plot them. 我想在一分钟内计算推文的数量,然后绘制它们。 But when I use
groupby().count()
this deletes the day, month, and year. 但是,当我使用
groupby().count()
这会删除日期,月份和年份。
I got back all tweets for minute 1, but I want to get the number of tweets for 14:29 2018-05-02
and then number of tweets for 14:30 2018-05-02
. 我在第一分钟取回了所有推文,但我想获取
14:29 2018-05-02
的推文数量,然后是14:30 2018-05-02
的推文数量。
With a pandas dataframe you can groupby
with a period of a minute and count
. 使用pandas数据框,您可以用一分钟的时间进行
groupby
并count
。 You need to make sure that time
is a datetime
field, eg: 您需要确保
time
是datetime
time
字段,例如:
df.time = pd.to_datetime(df.time)
Then simply: 然后简单地:
df.groupby(df.time.dt.to_period('Min')).count()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.