Home > Software engineering >  Pandas df create a column that adds up the last two numbers of a different column
Pandas df create a column that adds up the last two numbers of a different column

Time:04-25

Index Value Value sum
1 10 10
2 20 30
3 20 50
4 30 80
5 20 100

Something like that, it should be a lambda function I assume but I can't figure it out.

df['value sum'] = df['value']
df['value sum'].iloc[1] = df['value sum'].iloc[0]   df['value sum'].iloc[1]

This works if I keep pasting it for the whole dataframe, but it's not automatic.

CodePudding user response:

Based on your description and pseudo-code, it sounds like you want to use rolling (though this doesn't match your sample column values). rolling allows you to add the previous n rows in current, for example:

d = {'Index': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5},
 'Value': {0: 10, 1: 20, 2: 20, 3: 30, 4: 20},
 'Value sum': {0: 10, 1: 30, 2: 50, 3: 80, 4: 100}}

df = pd.DataFrame.from_dict(d)  

df['RollingSum'] = df.Value.rolling(window=3, min_periods=0).sum()

output:

   Index  Value  Value sum  RollingSum
0      1     10         10        10.0
1      2     20         30        30.0
2      3     20         50        50.0
3      4     30         80        70.0
4      5     20        100        70.0

(if you want Value sum, df.Value.cumsum() will work as pointed out in the comments.)

CodePudding user response:

import pandas as pd
value = [10, 20, 20, 30, 20]
sum = []
sum.append(value[0])
for x in range(1, len(value)):
    sum.append(sum[x - 1]   value[x])
d = {'value': value, 'sum': sum}
df = pd.DataFrame(data = d, index = range(1, 6))
df

CodePudding user response:

I think a solution like this:

import pandas as pd
df = pd.DataFrame([10,20,20,30,20], columns=['Value'])


value_sum = []
old_v = 0 
for v in df['Value']:
    value_sum.append(v old_v)
    old_v = v old_v
df['Value sum'] = value_sum


df

  Value  Value sum
0   10      10
1   20      30
2   20      50
3   30      80
4   20     100

I created a list that will be filled with the sum of the preceding value in the Value column. In the end, I assigned the list in a new column of the data frame called Value sum

  • Related