累积金额仅适用于1列python

时间:2017-03-01 08:09:13

标签: python pandas cumulative-sum

我想在1个特定列上应用cumsum,因为我在不同的列中有其他值必须保持不变。

这是我到目前为止的脚本

df.groupby(by=['name','day']).sum().groupby(level=[0]).cumsum()

然而,这个脚本导致我的pandas df中的所有列都会累积。必须累积总和的唯一列是data

根据要求,这里有一些示例数据:

df = pd.DataFrame({'ID': ["880022443344556677787", "880022443344556677782", "880022443344556677787",
                          "880022443344556677782", "880022443344556677787", "880022443344556677782",
                          "880022443344556677781"],
                   'Month': ["201701", "201701", "201702", "201702", "201703", "201703", "201703"],
                   'Usage': [20, 40, 100, 50, 30, 30, 2000],
                   'Sec': [10, 15, 20, 1, 5, 6, 30]})

                      ID   Month  Sec  Usage
0  880022443344556677787  201701   10     20
1  880022443344556677782  201701   15     40
2  880022443344556677787  201702   20    100
3  880022443344556677782  201702    1     50
4  880022443344556677787  201703    5     30
5  880022443344556677782  201703    6     30
6  880022443344556677781  201703   30   2000

期望的输出

                      ID   Month  Sec  Usage
0  880022443344556677787  201701   10     20
1  880022443344556677782  201701   15     40
2  880022443344556677787  201702   20    120
3  880022443344556677782  201702    1     90
4  880022443344556677787  201703    5    150
5  880022443344556677782  201703    6    120
6  880022443344556677781  201703   30   2000

3 个答案:

答案 0 :(得分:3)

考虑数据框df

df = pd.DataFrame(dict(
        name=list('aaaaaaaabbbbbbbb'),
        day=np.tile(np.arange(2).repeat(4), 2),
        data=np.arange(16)
    ))

首先,您通过在cumsum语句后命名列来对特定列执行groupby

其次,您可以使用df

将其添加回数据框join
d2 = df.groupby(['name', 'day']).data.sum().groupby(level=0).cumsum()

df.join(d2, on=['name', 'day'], rsuffix='_cum')

    data  day name  data_cum
0      0    0    a         6
1      1    0    a         6
2      2    0    a         6
3      3    0    a         6
4      4    1    a        28
5      5    1    a        28
6      6    1    a        28
7      7    1    a        28
8      8    0    b        38
9      9    0    b        38
10    10    0    b        38
11    11    0    b        38
12    12    1    b        92
13    13    1    b        92
14    14    1    b        92
15    15    1    b        92

答案 1 :(得分:2)

对于不需要cumsum的cols,我认为你需要set_index - 我通过list comprehension动态找到它们:

cumsum_col = 'Usage'
df1 = df.groupby(by=['ID','Month'], sort=False).sum()
cols = [col for col in df1.columns if col != cumsum_col]

df1 = df1.set_index(cols, append=True).groupby(level=[0]).cumsum().reset_index()
print (df1)
                      ID   Month  Sec  Usage
0  880022443344556677787  201701   10     20
1  880022443344556677782  201701   15     40
2  880022443344556677787  201702   20    120
3  880022443344556677782  201702    1     90
4  880022443344556677787  201703    5    150
5  880022443344556677782  201703    6    120
6  880022443344556677781  201703   30   2000

编辑:

cumsum_col = 'Usage'
df2 = df.groupby(by=['ID','Month'], sort=False).sum()
cols = [col for col in df2.columns if col != cumsum_col]
df1 = df2.set_index(cols, append=True).groupby(level=[0]).cumsum()
df1 = df2.assign(Usage_cumsum = df1.reset_index(level=2, drop=True)).reset_index()
print (df1)
                      ID   Month  Sec  Usage  Usage_cumsum
0  880022443344556677787  201701   10     20            20
1  880022443344556677782  201701   15     40            40
2  880022443344556677787  201702   20    100           120
3  880022443344556677782  201702    1     50            90
4  880022443344556677787  201703    5     30           150
5  880022443344556677782  201703    6     30           120
6  880022443344556677781  201703   30   2000          2000

EDIT1:

在您的示例中,数据不是聚合sum,因此数据有点修改(解决方案类似,但与另一个不同):

df = pd.DataFrame({'ID': ["880022443344556677787", "880022443344556677782", "880022443344556677787",
                          "880022443344556677782", "880022443344556677787", "880022443344556677782",
                          "880022443344556677781"],
                   'Month': ["201701", "201701", "201701", "201702", "201703", "201701", "201703"],
                   'Usage': [20, 40, 100, 50, 30, 30, 2000],
                   'Sec': [10, 15, 20, 1, 5, 6, 30]})

print (df)
                      ID   Month  Sec  Usage
0  880022443344556677787  201701   10     20
1  880022443344556677782  201701   15     40
2  880022443344556677787  201701   20    100
3  880022443344556677782  201702    1     50
4  880022443344556677787  201703    5     30
5  880022443344556677782  201701    6     30
6  880022443344556677781  201703   30   2000
#aggregate sum to all columns
df1 = df.groupby(['ID', 'Month']).sum() 
print (df1)
                              Sec  Usage
ID                    Month             
880022443344556677781 201703   30   2000
880022443344556677782 201701   21     70
                      201702    1     50
880022443344556677787 201701   30    120
                      201703    5     30

#aggregate cumcum to Usage column only 
s = df1.groupby(level=0)['Usage'].cumsum()
print (s)
ID                     Month 
880022443344556677781  201703    2000
880022443344556677782  201701      70
                       201702     120
880022443344556677787  201701     120
                       201703     150
Name: Usage, dtype: int64
#join cumsum series to aggregate df1
df3 = df1.join(s, rsuffix='_cumsum').reset_index()
print (df3)
                      ID   Month  Sec  Usage  Usage_cumsum
0  880022443344556677781  201703   30   2000          2000
1  880022443344556677782  201701   21     70            70
2  880022443344556677782  201702    1     50           120
3  880022443344556677787  201701   30    120           120
4  880022443344556677787  201703    5     30           150

答案 2 :(得分:1)

您已经可以将累积总和('cumsum')作为df.groupby的聚合。您需要将'cumsum'作为字符串作为聚合函数提供给“数据”列。

df.groupby(['name','day']).agg({'data': 'cumsum'})