pythonpandasfor-looppartition-by

How to create a new column takes the value from the sum of another column each row?


Like taking the value from the first row 60, then add it to the new value from the next row and so on, like the example of the below table.

     field       A                             B
0      u       60.0                           60.0
1      v       78.0   >>     60.0  + 78.0  =  138.0
2      w       42.0   >>     138.0 + 42.0  =  180.0
3      x       61.0   >>     180.0 + 61.0  =  241.0
4      y       36.0   >>     241.0 + 36.0  =  277.0

Solution

  • Pandas has a built-in "cumulative sum"

    import pandas as pd
    
    df = pd.DataFrame(
        {
            "field": ["u", "v", "w", "x", "y"],
            "A": [60, 78, 42, 61, 36],
        }
    )
    
    df["B"] = df["A"].cumsum()
    
    print(df)
    

    Output:

      field   A    B
    0     u  60   60
    1     v  78  138
    2     w  42  180
    3     x  61  241
    4     y  36  277