Python Pandas:生成一个新列,计算特定列中该行上方所有单元格的小计

Python Pandas: Generate a new column that calculates the subtotal of all the cells above that row in a specific column

对于看似令人困惑的标题,我们深表歉意。问题应该很简单,但我很困惑,需要一些帮助。

我现在的数据框:

New_ID  STATE   MEAN
0   1   Lagos   7166.101571
1   2   Rivers  2464.065846
2   3   Oyo     1974.699365
3   4   Akwa    1839.126698
4   5   Kano    1757.642462

我想在第 i 行创建一个新列,它将计算 df[:i,'MEAN'].sum()/df['MEAN'].sum()

例如,对于数据框:

    ID  MEAN
0   1.0 5
1   2.0 10
2   3.0 15
3   4.0 30
4   5.0 40

我想要的输出:

     ID MEAN SUBTOTAL
0   1.0 5   0.05
1   2.0 10  0.10
2   3.0 15  0.30
3   4.0 30  0.60
4   5.0 40  1.00

我试过了

df1['SUbTotal'] = df1.loc[:df1['New_ID'], 'MEAN']/df1['MEAN'].sum()

但上面写着:

Name: New_ID, dtype: int32' is an invalid key 

提前感谢您抽出时间

这个应该可以,看来你在找cumsum:

df['SUBTOTAL'] = df.MEAN.cumsum() / df.MEAN.sum()

>>> df
    ID  MEAN  SUBTOTAL
0  1.0     5      0.05
1  2.0    10      0.15
2  3.0    15      0.30
3  4.0    30      0.60
4  5.0    40      1.00