Python 在熊猫中按周分组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45281297/
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
group by week in pandas
提问by Ram
I'm having this data frame:
我有这个数据框:
Name Date Quantity
Apple 07/11/17 20
orange 07/14/17 20
Apple 07/14/17 70
Orange 07/25/17 40
Apple 07/20/17 30
I want to aggregate this by Name and Date to get sum of quantities Details:
我想按名称和日期汇总它以获得数量总和详细信息:
Date: Group, the result should be at the beginning of the week (or just on Monday)
日期:组,结果应该是在一周的开始(或就在星期一)
Quantity: Sum, if two or more record have same Name and Date(if falls on same interval)
数量:总和,如果两个或多个记录具有相同的名称和日期(如果属于相同的间隔)
The desired output is given below:
所需的输出如下:
Name Date Quantity
Apple 07/10/17 90
orange 07/10/17 20
Apple 07/17/17 30
orange 07/24/17 40
Thanks in advance
提前致谢
回答by jezrael
First convert column date
to_datetime
and substract one week.
首先转换列date
to_datetime
并减去一周。
Then use groupby
with Grouper
by W-MONand aggregate sum
:
然后使用groupby
with Grouper
by W-MON和聚合sum
:
df['Date'] = pd.to_datetime(df['Date']) - pd.to_timedelta(7, unit='d')
df = df.groupby(['Name', pd.Grouper(key='Date', freq='W-MON')])['Quantity']
.sum()
.reset_index()
.sort_values('Date')
print (df)
Name Date Quantity
0 Apple 2017-07-10 90
3 orange 2017-07-10 20
1 Apple 2017-07-17 30
2 Orange 2017-07-24 40
回答by Scott Boston
Let's use groupby
, resample
with W-Mon
, and sum
:
让我们使用groupby
,resample
与W-Mon
和sum
:
df.groupby('Name').resample('W-Mon', on='Date').sum().reset_index().sort_values(by='Date')
Output:
输出:
Name Date Quantity
0 Apple 2017-07-17 90
3 orange 2017-07-17 20
1 Apple 2017-07-24 30
2 Orange 2017-07-31 40
回答by LN_P
First convert column date to_datetime. This will group by week starting with Mondays. It will output the week number (but you can change that looking up in
首先将列日期转换为_datetime。这将从星期一开始按周分组。它将输出周数(但您可以在
df.groupby(['name', df['date'].dt.strftime('%W')])['quantity'].sum()
Output:
输出:
name date
apple 28 90
29 30
orange 28 20
30 40