简体   繁体   English

按分钟,小时,天,月和年分组?

[英]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数据框,您可以用一分钟的时间进行groupbycount You need to make sure that time is a datetime field, eg: 您需要确保timedatetime 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.

相关问题 python中的时间戳(年、月、日、小时、分钟) - Timestamp in python (year,month,day,hour,minute) 用Python解析年,月,日,时,分,秒 - Parsing year, month, day, hour, minute, second in Python 从当前日期时间中提取年、月、日、小时和分钟 - Extracting year, month, day, hour and minute from the current datetime 如何为 Pandas 中的 2 天数据从年、日、小时和分钟列(无月列)创建日期时间 object? - How to create a datetime object from Year, Day, Hour and Minute columns (without month column) for 2 day data in Pandas? 如何在python中获取当前时间并分解为年、月、日、小时、分钟? - How to get current time in python and break up into year, month, day, hour, minute? 如何从日期时间对象中提取小时/分钟并去掉年/月/日/秒部分? - How do I extract the Hour/Minute from a datetime object and get rid of the Year/Month/Day/Second section? 如何指定在大熊猫(小时,分钟,秒,日,月,年)中读取时间值时首先出现的内容? - How to specify what comes first when reading time values in pandas (hour, minute, seconds, day, month, year)? 如何为年/月/日/小时/分钟/秒创建日期时间的Pandas列? - How to create a Pandas column for datetime from year / month/ day / hour / minute / second? Python:如何以年月日时分秒格式转换谷歌位置时间戳? - Python: How to convert google location timestaMps in a year-month-day-hour-minute-seconds format? Python datetime - 在使用 strptime 获取日、月、年之后设置固定的小时和分钟 - Python datetime - setting fixed hour and minute after using strptime to get day,month,year
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM