Pandas's DataFrame multiline summation problem.

< H2 > Development environment < / H2 >

python 3.6
pandas 0.23.3

has a DataFrame, as follows:

import pandas as pd
arr = [[10,10,1],[22,24.2,1.1],[15,15,1],[9,8.1,0.9],[50,55,1.1]]
df = pd.DataFrame(arr,columns=["volume","amount","price"])
    volume  amount  price
0      10    10.0    1.0
1      22    24.2    1.1
2      15    15.0    1.0
3       9     8.1    0.9
4      50    55.0    1.1

requires the sum of each row of the volume column to get the total column

    volume  amount  price  total
0      10    10.0    1.0    10.0
1      22    24.2    1.1    32.0
2      15    15.0    1.0    47.0
3       9     8.1    0.9    56.0
4      50    55.0    1.1    106.0

my current way of writing:

for index in df.index:
     num = df[df.index < index]["volume"].sum()
     df.loc["total"] = num

is there a better way to turn to the gods?

Aug.05,2021

df['total'] = df['volume'].cumsum()
Menu