Skip to content
Advertisement

sum of row in the same columns in pandas

i have a dataframe something like this

   d1  d2    d3    d4
  780  37.0  21.4  122840.0
  784  38.1  21.4  122860.0
  846  38.1  21.4  122880.0
  843  38.0  21.5  122900.0
  820  36.3  22.9  133220.0
  819  36.3  22.9  133240.0
  819  36.4  22.9  133260.0
  820  36.3  22.9  133280.0
  822  36.4  22.9  133300.0

how do i get the sum of values between the same column in a new column in a dataframe for example:

 d1    d2    d3    d4       d5 
780  37.0  21.4  122840.0  1564
784  38.1  21.4  122860.0  1630
846  38.1  21.4  122880.0  1689

i want a new column with the sum of d1[i] + d1[i+1] .i know .sum() in pandas but i cant do sum between the same column

Advertisement

Answer

Your question is not fully clear to me, but I think what you mean to do is:

df['d5'] = df['d1'] + df['d1'].shift(-1)

Now you have to decide what you want to happen for the last element of the series.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement