Python Pandas 使用日期时间数据按日期分组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39400115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Pandas Group by date using datetime data
提问by GoBlue_MathMan
I have a column Date_Time
that I wish to groupby date time without creating a new column. Is this possible the current code I have does not work.
我有一个列Date_Time
,我希望在不创建新列的情况下按日期时间分组。这可能是我当前的代码不起作用。
df = pd.groupby(df,by=[df['Date_Time'].date()])
采纳答案by piRSquared
resample
resample
df.resample('D', on='Date_Time').mean()
B
Date_Time
2001-10-01 4.5
2001-10-02 6.0
Grouper
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
回答by jezrael
You can use groupby
by dates of column Date_Time
by dt.date
:
您可以groupby
按列Date_Time
的日期使用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