graceli
graceli

Reputation: 45

How to calculate the sum of two adjacent row from one column?

I have a data frame with the following columns, the first column is index:

para
0  223.46
1   92.26
2   66.86
3   52.14
4   69.55
5   94.20
6  129.96
7  297.48

The sum will be the two adjacent row from one column new_index 0 will be the first value, new_index1= old_index0+old_index1, new_index2=old_index1 + old_index2 ,......and so on.

so I guess I need a for loop here(or maybe not)

I tried several ways, really have no idea how to do it. The follow is what I tried:

def sum(i):
for i in range (0,i):
    sum = data_10.icol[i] + data_10.icol[i+1]
return sum

I excepted to get:

para
0 223.46
1 315.72
2 159.12
3 119.00
4 121.69
5 163.75
6 224.16
7 427.38

Upvotes: 3

Views: 657

Answers (1)

BENY
BENY

Reputation: 323356

This is rolling sum

df.rolling(2,min_periods=1).sum()

Upvotes: 4

Related Questions