简体   繁体   English

"如何获得落在 Pandas df 滑动窗口内的日期列表的总和和重叠?"

[英]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:我有一个这样的df:

date日期<\/th> name名称<\/th> amount数量<\/th><\/tr><\/thead>
2021-07-01 2021-07-01<\/td> 'Chlorox' '氯氧'<\/td> 1 1<\/td><\/tr>
2021-07-14 2021-07-14<\/td> 'Chlorox' '氯氧'<\/td> 20 20<\/td><\/tr>
2021-07-29 2021-07-29<\/td> 'Chlorox' '氯氧'<\/td> 700 700<\/td><\/tr>
2021-08-11 2021-08-11<\/td> 'Chlorox' '氯氧'<\/td> 6000 6000<\/td><\/tr>
2021-08-12 2021-08-12<\/td> 'Suriname' '苏里南'<\/td> 3 3<\/td><\/tr>
2021-08-19 2021-08-19<\/td> 'Suriname' '苏里南'<\/td> 10 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 721<\/td> ['2021-07-01', '2021-07-14', '2021-07-29'] ['2021-07-01', '2021-07-14', '2021-07-29']<\/td><\/tr>
'Chlorox' '氯氧'<\/td> 6720 6720<\/td> '2021-07-14', '2021-07-29', '2021-08-11' '2021-07-14'、'2021-07-29'、'2021-08-11'<\/td><\/tr>
'Suriname' '苏里南'<\/td> 13 13<\/td> ['2021-08-12', '2021-08-19'] ['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!我一直在修改 rolling() 和 groupby,但我一直在苦苦挣扎,无法获得重叠的日期!

"

Use pd.to_datetime<\/code><\/a> , Series.dt.to_period<\/code><\/a> with Groupby.agg<\/code><\/a> :使用pd.to_datetime<\/code><\/a> , Series.dt.to_period<\/code><\/a>和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> .我认为目标是根据month<\/code>和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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过查看一个数据框中的日期落在另一个数据框中的日期范围内来合并Pandas数据框? - How do I combine Pandas dataframes by looking at dates in one dataframe that fall within a date range in another dataframe? 熊猫df中的值总和 - Sum list of values within a pandas df 如何获取 pandas df 中多个列的重叠最小和最大日期 - how to obtain overlapping min and max dates for multiple columns in a pandas df Python - 给定日期列表(作为字符串),我们如何仅返回过去 365 天内的日期? - Python - Given list of dates (as strings), how do we return only those that fall within last 365 days? 如何使用在 python 中作为变量输入的日期从指定行内的 csv 中获取列的总和? - How do I get the sum of columns from a csv within specified rows using dates inputting as variables in python? 在熊猫中,如果一个字段中的日期与日期列表中的任何日期匹配,我如何标记数据框中的一行? - In pandas, how do I flag a row in a dataframe if a date in one field matches any date within a list of dates? 如何将 Pandas DF 中的列表转换为字符串? - How do I convert a list in a Pandas DF into a string? 如何检查一个 pandas dataframe 中的日期是否在另一个日期范围内? - How to check if dates in one pandas dataframe fall within ranges of dates in another? 熊猫Df总和groupby列的所有日期 - Pandas df sum groupby column for all dates 如果列值是日期列表,如何按条件计算 pandas DF 中的行数? - How to count rows in pandas DF on condition, if column value is a list of dates?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM