[英]Python Pandas - Cumulative sum by date (Month-Year) and unique user
I would like to add the cumulative daily sum of the column ['PR'] by month-year while also grouping by ['PERMNO'].我想按月/年添加列 ['PR'] 的累积每日总和,同时按 ['PERMNO'] 分组。
Here is a snippet of my dataframe:这是我的数据框的片段:
PERMNO date PR
0 10025 2012-09-17 0
1 10025 2012-09-18 0
2 10025 2012-09-19 0
3 10025 2012-09-20 1
4 10025 2012-09-21 0
... ... ... ...
8567613 93436 2017-12-24 1
8567614 93436 2017-12-26 1
8567615 93436 2017-12-29 0
8567616 93436 2017-12-30 1
8567617 93436 2017-12-31 0
I would like to get something like this:我想得到这样的东西:
PERMNO date PR
0 10025 2012-09 10
1 10025 2012-10 6
2 10025 2012-11 4
3 10025 2012-12 20
4 10073 2012-01 5
... ... ... ...
n-4 93436 2017-08 15
n-3 93436 2017-09 17
n-2 93436 2017-10 4
n-1 93436 2017-11 13
n 93436 2017-12 10
I'm mainly using Pandas and NumPy.我主要使用 Pandas 和 NumPy。
Thank you in advance.先感谢您。
Here is the result when summing by PERMNO and year-month, though the expected result in the question does not match the provided data这是按PERMNO和年月求和时的结果,尽管问题中的预期结果与提供的数据不匹配
df['date'] = pd.to_datetime(df['date'])
df.groupby(['PERMNO', df['date'].dt.strftime('%Y-%m') ]).sum().reset_index()
PERMNO date PR
0 10025 2012-09 1
1 93436 2017-12 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.