用groupby python更改百分比

时间:2018-09-22 15:07:03

标签: python pandas

我有以下数据框:

Year    Month   Booked
0   2016    Aug 55999.0
6   2017    Aug 60862.0
1   2016    Jul 54062.0
7   2017    Jul 58417.0
2   2016    Jun 42044.0
8   2017    Jun 48767.0
3   2016    May 39676.0
9   2017    May 40986.0
4   2016    Oct 39593.0
10  2017    Oct 41439.0
5   2016    Sep 49677.0
11  2017    Sep 53969.0

我想获得相对于去年同月的百分比变化。我尝试了以下代码:

df['pct_ch'] = df.groupby(['Month','Year'])['Booked'].pct_change()

但是我得到以下信息,这根本不是我想要的:

Year    Month   Booked  pct_ch
0   2016    Aug 55999.0 NaN
6   2017    Aug 60862.0 0.086841
1   2016    Jul 54062.0 -0.111728
7   2017    Jul 58417.0 0.080556
2   2016    Jun 42044.0 -0.280278
8   2017    Jun 48767.0 0.159904
3   2016    May 39676.0 -0.186417
9   2017    May 40986.0 0.033017
4   2016    Oct 39593.0 -0.033987
10  2017    Oct 41439.0 0.046624
5   2016    Sep 49677.0 0.198798
11  2017    Sep 53969.0 0.086398

1 个答案:

答案 0 :(得分:0)

请勿groupby 年份,否则您将不会一起获得Aug 2017Aug 2016。另外,使用transform将结果广播回原始索引

尝试:

df['pct_ch'] = df.groupby(['Month'])['Booked'].transform(lambda s: s.pct_change())

    Year    Month   Booked  pct_ch
0   2016    Aug     55999.0 NaN
6   2017    Aug     60862.0 0.086841
1   2016    Jul     54062.0 NaN
7   2017    Jul     58417.0 0.080556
2   2016    Jun     42044.0 NaN
8   2017    Jun     48767.0 0.159904
3   2016    May     39676.0 NaN
9   2017    May     40986.0 0.033017
4   2016    Oct     39593.0 NaN
10  2017    Oct     41439.0 0.046624
5   2016    Sep     49677.0 NaN
11  2017    Sep     53969.0 0.086398