[英]Python Pandas groupby month day year week
I have a pandas DataFrame that looks like this: 我有一个熊猫DataFrame看起来像这样:
Day Month Year Date Week Data1 Data2
1 January 2016 01-01-2016 1 15 22
2 January 2016 01-02-2016 1 16 26
12 January 2016 01-12-2016 2 18 29
8 February 2016 02-08-2016 2 9 17
I manually added the week column and they are type int. 我手动添加了周列,它们的类型为int。 I want to group the data by week, month, year ie: January 2016 week 1, January 2016 week 2, February 2016 week 2. The issue is I want the result to be a DataFrame where Data1 is the sum of the columns and Data2 is the mean of the columns.
我想按周,月,年对数据进行分组,即:2016年1月第1周,2016年1月第2周,2016年2月第2周。问题是我希望结果是一个DataFrame,其中Data1是列和Data2的总和是列的平均值。
Thank you for the help. 感谢您的帮助。
I think you can use groupby
followed by agg
for your desired aggregations. 我认为您可以在
groupby
之后使用agg
进行所需的聚合。
df.groupby(['Year', 'Month', 'Week']).agg({'Data1':'sum', 'Data2':'mean'})
Demo 演示
>>> df.groupby(['Year', 'Month', 'Week']).agg({'Data1':'sum', 'Data2':'mean'})
Data1 Data2
Year Month Week
2016 February 2 9 17
January 1 31 24
2 18 29
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.