ObiwanKeTobi
ObiwanKeTobi

Reputation: 21

add new column with the summary of all previous values (python)

I am very new to python and trying to complete an appointment for uni. I've already tried googling the issue (and there may already be a solution) but could not find a solution to my problem.

I have a dataframe with values and a timestamp. It looks like this:

created_at delta
2020-01-01 1.45
2020-01-02 0.12
2020-01-03 1.01
... ...

I want to create a new column 'sum' which summarizes all the previous values, like this:

created_at delta sum
2020-01-01 1.45 1.45
2020-01-02 0.12 1.57
2020-01-03 1.01 2.58
... ... ...

I want to define a method that I can use on different files (the data is spread across multiple files).

I have tried this but it doesn't work

def sum_ (data_index):
    df_sum = delta_(data_index) #getting the data
    y = len(df_sum)   
    for x in range(0,y):
        df_sum['sum'].iloc[[0]] = df_sum['delta'].iloc[[0]]
        df_sum['sum'].iloc[[x]] = df_sum['sum'].iloc[[x-1]] + df_sum['delta'].iloc[[x]]

    return df_sum

For any help, I am very thankful.

Kind regards

Upvotes: 1

Views: 358

Answers (2)

Artem Mashkovskiy
Artem Mashkovskiy

Reputation: 131

Try cumsum():

df['sum'] = df['delta'].cumsum()

Upvotes: 2

Daweo
Daweo

Reputation: 36520

Use cumsum simple example

import pandas as pd
df = pd.DataFrame({'x':[1,2,3,4,5]})
df['y'] = df['x'].cumsum()
print(df)

output

   x   y
0  1   1
1  2   3
2  3   6
3  4  10
4  5  15

Upvotes: 1

Related Questions