简体   繁体   English

Python Pandas 使用日期时间数据按日期分组

[英]Python Pandas Group by date using datetime data

I have a column Date_Time that I wish to groupby date time without creating a new column.我有一个Date_Time列,我希望在不创建新列的情况下按日期时间Date_Time Is this possible the current code I have does not work.这可能是我当前的代码不起作用。

df = pd.groupby(df,by=[df['Date_Time'].date()])

You can use groupby by dates of column Date_Time by dt.date :您可以使用groupby by 列Date_Time by dt.date日期:

df = df.groupby([df['Date_Time'].dt.date]).mean()

Sample:样本:

df = pd.DataFrame({'Date_Time': pd.date_range('10/1/2001 10:00:00', periods=3, freq='10H'),
                   'B':[4,5,6]})

print (df)
   B           Date_Time
0  4 2001-10-01 10:00:00
1  5 2001-10-01 20:00:00
2  6 2001-10-02 06:00:00

print (df['Date_Time'].dt.date)
0    2001-10-01
1    2001-10-01
2    2001-10-02
Name: Date_Time, dtype: object

df = df.groupby([df['Date_Time'].dt.date])['B'].mean()
print(df)
Date_Time
2001-10-01    4.5
2001-10-02    6.0
Name: B, dtype: float64

Another solution with resample :使用resample另一种解决方案:

df = df.set_index('Date_Time').resample('D')['B'].mean()

print(df)
Date_Time
2001-10-01    4.5
2001-10-02    6.0
Freq: D, Name: B, dtype: float64

resample

df.resample('D', on='Date_Time').mean()

              B
Date_Time      
2001-10-01  4.5
2001-10-02  6.0

Grouper

As suggested by @JosephCottam正如@JosephCottam 所建议的

df.set_index('Date_Time').groupby(pd.Grouper(freq='D')).mean()

              B
Date_Time      
2001-10-01  4.5
2001-10-02  6.0

Deprecated uses of TimeGrouper不推荐使用TimeGrouper

You can set the index to be 'Date_Time' and use pd.TimeGrouper您可以将索引设置为'Date_Time'并使用pd.TimeGrouper

df.set_index('Date_Time').groupby(pd.TimeGrouper('D')).mean().dropna()

              B
Date_Time      
2001-10-01  4.5
2001-10-02  6.0

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM