简体   繁体   中英

How do I get both the sum and an overlapping a list of dates that fall within a sliding window in a Pandas df?

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:

name<\/th> sum<\/th> dates<\/th><\/tr><\/thead>
'Chlorox' <\/td> 721 <\/td> ['2021-07-01', '2021-07-14', '2021-07-29'] <\/td><\/tr>
'Chlorox' <\/td> 6720 <\/td> '2021-07-14', '2021-07-29', '2021-08-11' <\/td><\/tr>
'Suriname' <\/td> 13 <\/td> ['2021-08-12', '2021-08-19'] <\/td><\/tr><\/tbody><\/table>

I've been tinkering around with rolling() and groupby, but I've been struggling and unable to get overlapping dates!

"

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM