I have a df like this:
date<\/th> | name<\/th> | amount<\/th><\/tr><\/thead> | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2021-07-01 <\/td> | 'Chlorox' <\/td> | 1 <\/td><\/tr> | ||||||||||||
2021-07-14 <\/td> | 'Chlorox' <\/td> | 20 <\/td><\/tr> | ||||||||||||
2021-07-29 <\/td> | 'Chlorox' <\/td> | 700 <\/td><\/tr> | ||||||||||||
2021-08-11 <\/td> | 'Chlorox' <\/td> | 6000 <\/td><\/tr> | ||||||||||||
2021-08-12 <\/td> | 'Suriname' <\/td> | 3 <\/td><\/tr> | ||||||||||||
2021-08-19 <\/td> | 'Suriname' <\/td> | 10 <\/td><\/tr><\/tbody><\/table> and I'd like the sum of amounts within a one month period, plus all the dates that fall within that month range. So something like these results:
|
Use pd.to_datetime<\/code><\/a> ,
Series.dt.to_period<\/code><\/a> with
Groupby.agg<\/code><\/a> :
In [874]: df['date'] = pd.to_datetime(df['date']) # Convert date column to pandas datetime
In [923]: res = df.groupby(['name', df['date'].dt.to_period('M')], as_index=False).agg({'amount': sum, 'date': lambda x: list(x.dt.date)})
In [924]: res
Out[924]:
name amount date
0 'Chlorox' 721 [2021-07-01, 2021-07-14, 2021-07-29]
1 'Chlorox' 6000 [2021-08-11]
2 'Suriname' 13 [2021-08-12, 2021-08-19]
I think the objective is to group by the columns based on the month<\/code> and
name<\/code> .
So the result dataframe will look like this -
date amount
name Month
Chlorox 7 2021-07-01, 2021-07-14, 2021-07-29 721
8 2021-08-11 6000
Suriname 8 2021-08-12, 2021-08-19 13
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.