Pandas - 汇总列

时间:2016-08-19 02:15:51

标签: python pandas

大熊猫新手,我试图总结一下列的所有先前值。在SQL中我通过将表连接到自身来做到这一点,所以我在大熊猫中采用了相同的方法,但遇到了一些问题。

原始数据框

         TeamName  PlayerCount   Goals   CalMonth
    0    A         25            126     1
    1    A         25            100     2
    2    A         25            156     3
    3    B         22            205     1
    4    B         30            300     2
    5    B         28            189     3

代码

    prev_month = np.where(df3['CalMonth'] == 12, df3['CalMonth'] - 11, df3['CalMonth'] + 1)

    df4 = pd.merge(df3, df3, how='left', left_on=['TeamName','CalMonth'], right_on=['TeamName', prev_month])
    print(df4.head(20))

输出

         TeamName  PlayerCount_x Goals_x CalMonth_x 
    0    A         25            126     1
    1    A         25            100     2
    2    A         25            156     3
    3    B         22            205     1
    4    B         22            300     2
    5    B         22            189     3

    PlayerCount_y Goals_y CalMonth_y 
    NaN           NaN     NaN
    25            126     1
    25            100     2
    22            NaN     NaN
    22            205     1
    22            100     2

输出是我的想法,但我现在想要的是创建一个YTD列并总结前几个月的所有目标。以下是我想要的结果(可以包括当前月份,也可以包括其他步骤):

         TeamName  PlayerCount_x Goals_x CalMonth_x 
    0    A         25            126     1
    1    A         25            100     2
    2    A         25            156     3
    3    B         22            205     1
    4    B         22            300     2
    5    B         22            189     3

    PlayerCount_y Goals_y CalMonth_y Goals_YTD
    NaN           NaN     NaN        NaN
    25            126     1          126
    25            100     2          226
    22            NaN     NaN        NaN
    22            205     1          205
    22            100     2          305

0 个答案:

没有答案