简体   繁体   English

从日期时间开始按日期分组的列的总和如何? Python Pandas

[英]How do the sum of a column with group by date from datetime ? Python Pandas

I would like to do the sum of the column duration group by date but the column begin and end are datetime in this piece of df:我想按日期对列持续时间组求和,但列开始和结束是这段 df 中的日期时间:

begin                       end                         duration
2020-10-14 19:17:52.724020  2020-10-14 19:21:40.179003  227.45
2020-10-14 19:21:40.179003  2020-10-14 19:21:44.037103  3.86
2020-10-14 19:59:27.183161  2020-10-14 20:00:43.847816  76.66
2020-10-14 20:00:43.847816  2020-10-14 20:00:43.847822  0
2020-10-14 20:02:14.341240  2020-10-14 23:59:59.900000  14265.56
2020-10-15 00:00:00.000000  2020-10-15 05:25:32.935971  19532.94
2020-10-15 05:25:32.935971  2020-10-15 05:25:33.068959  0.13

df.info() df.info()

begin       41763 non-null  datetime64[ns] 
end         41763 non-null  datetime64[ns] 
duration    41763 non-null  float64   

The result must be:结果必须是:

begin         duration
2020-10-14    14,573.53
2020-10-15    19,533.07

So I tried on my all df, this but its works for certain date and no for other.所以我尝试了我所有的 df,但它在特定日期有效,而在其他日期无效。 Because I do the same with excel and for a date I have a different result.因为我对 excel 做了同样的事情,而对于一个日期,我得到了不同的结果。

import pandas as pd
import datetime

df = df.groupby(df['begin_'].dt.date)['duration_'].sum()/3600

You can use the method date of the datetime object. Apply it to the column and you get the date.您可以使用日期时间 object 的方法date 。将其应用于该列并获得日期。 Afterwards grouping is fine.之后分组就好了。

def reduce_to_date(value):
    return value.date()

df['begin'] = df['begin'].apply(reduce_to_date)

df.groupby('begin')['duration'].sum()/3600

The first step is to separate Time and Date in the timestamp you have.第一步是在您拥有的时间戳中分隔时间和日期。 I give below and example where the dates are defined the same way they are defined in your dataframe.我在下面给出了日期定义方式与在您的 dataframe 中定义的方式相同的示例。

0   2018-07-02 10:54:00 227.45
1   2018-07-02 10:54:00 3.86
2   2018-07-02 10:54:00 76.66
3   2018-07-02 10:54:00 14265.56
4   2018-07-02 10:54:00 19532.94

d ={'DATA':['2018-07-02 10:54:00','2018-07-02 10:54:00' , '2018-07-02 10:54:00' , '2018-07-02 10:54:00' ,'2018-07-02 10:54:00'],'duration': [227.45,3.86,76.66,14265.56,19532.94]}  
DF = df.assign(Date=df.Date.dt.date, Time=df.Date.dt.time, Duration = df.duration)

The next step is to groupby the way you did it, but by simple give information about which variable you group by:下一步是按照你groupby的方式分组,但是通过简单地提供关于你分组的变量的信息:

DF.groupby(['Date']).sum()

which give这给

Date        Duration     duration
2018-07-02  34106.47    34106.47

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在python中按日期和总和值对非唯一日期时间列进行分组 - Group non-unique datetime column by date and sum values in python Pandas:如何按日期时间列分组,仅使用时间并丢弃日期 - Pandas: How to group by a datetime column, using only the time and discarding the date 如何将日期从文件名添加到时间列以创建datetime列? 蟒蛇熊猫 - How to add a date from filename to a time column to make datetime column? Python Pandas Python Pandas 使用日期时间数据按日期分组 - Python Pandas Group by date using datetime data 将列从 Pandas 日期 object 更改为 python 日期时间 - Change column from Pandas date object to python datetime 如何使用 Pandas (Python) 对行进行分组并对列中的数字求和 - How to group Row and sum the number in Column using Pandas (Python) 如何从日期栏中获取每个股票的年数总和?(熊猫) - How do I get the sum of years from date column in reference to each ticker?(Pandas) 如何在 Python/Pandas 中将 if then 语句与 group by sum 运算符结合使用? - How to do a if then statement in conjunction with a group by sum operator in Python/Pandas? 如何在python的熊猫中滚动一周的最新总和 - How to do a rolling week-to-date sum in python's Pandas 如何保留 pandas 石斑鱼和分组依据中的日期时间列? - How to retain datetime column in pandas grouper and group by?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM