簡體   English   中英

如何使用過濾器和日期轉換在Python中按聚合進行分組

[英]How to do a group by aggregation in Python with filter and date transformation

我有下面的數據集,想要對基於供應商和從日期開始的月份的值進行求和,同時還要應用一個僅返回每月第一個星期一的過濾器。

date      vendor   value
07/01/19  Amazon   10
07/01/19  Amazon   500
04/02/19  ebay     60
04/03/19  Amazon   130
06/03/19  ebay     20
25/03/19  pcworld  250

我相信熊貓將是最好的前進方式,但我是python的新手,所以不知道。

vendor  month   value
Amazon  1       510
Amazon  3       130
ebay    2       60

您可以這樣做:

df['date'] = pd.to_datetime(df['date'], dayfirst=True)
#You data appears to be dayfirst

df_filt = df.where((df['date'].dt.dayofweek == 0) & (df['date'].dt.day < 8)).dropna(how='all')
#Filter out all data whre date isn't on monday nor in the first seven day of a month
df_fil.groupby(['vendor',df_fil['date'].dt.month])['value'].agg('sum').reset_index().rename(columns={'date':'month'})
#groupby with agg

輸出:

   vendor  month  value
0  Amazon      1  510.0
1  Amazon      3  130.0
2    ebay      2   60.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM