反转累计列

时间:2018-04-10 13:00:44

标签: python pandas pandas-groupby cumsum

我想反过来 - Calculating YTD totals in Pandas

ID  month   year    Fee
68119   February    2015    25
68119   March   2015    25
68119   April   2015    25
68119   May 2015    25
00200   February    2015    50
00200   March   2015    375
00200   April   2015    375
00200   May 2015    375
00200   June    2015    375
00200   July    2015    375
00200   August  2015    375

Fee年初至今的数字。我想要MTD号码。预期产出 -

ID  month   year    Fee
68119   February    2015    25
68119   March   2015    0
68119   April   2015    0
68119   May 2015    0
00200   February    2015    50
00200   March   2015    325
00200   April   2015    0
00200   May 2015    0
00200   June    2015    0
00200   July    2015    0
00200   August  2015    0

YTD的逻辑 -

df.groupby('ID')['Fee'].cumsum()

现在对于MTD,我认为如果没有pandas方式,那么我必须在IDsort上使用for循环[year, month] 1}}然后进行减法。我尝试过,但我知道有一种更原生的pandas方式。提前谢谢。

1 个答案:

答案 0 :(得分:3)

我们试试这个:

df['FeeMonthly'] = df.groupby(['ID','year'])['Fee'].diff().fillna(df.Fee)

输出:

       ID     month  year  Fee  FeeMonthly
0   68119  February  2015   25        25.0
1   68119     March  2015   25         0.0
2   68119     April  2015   25         0.0
3   68119       May  2015   25         0.0
4     200  February  2015   50        50.0
5     200     March  2015  375       325.0
6     200     April  2015  375         0.0
7     200       May  2015  375         0.0
8     200      June  2015  375         0.0
9     200      July  2015  375         0.0
10    200    August  2015  375         0.0