MissBleu
MissBleu

Reputation: 175

opposite of df.diff() in pandas

I have searched the forums in search of a cleaner way to create a new column in a dataframe that is the sum of the row with the previous row- the opposite of the .diff() function which takes the difference.

this is how I'm currently solving the problem

df = pd.DataFrame ({'c':['dd','ee','ff', 'gg', 'hh'], 'd':[1,2,3,4,5]}
df['e']= df['d'].shift(-1)
df['f'] = df['d'] + df['e']

Your ideas are appreciated.

Upvotes: 9

Views: 7378

Answers (3)

user59271
user59271

Reputation: 430

If you cannot use rolling, due to multindex or else, you can try using .cumsum(), and then .diff(-2) to sub the .cumsum() result from two positions before.

data = {'a':[1,6,3,9,5,30, 101, 8]}
df = pd.DataFrame(data)
df['opp_diff'] = df['a'].cumsum().diff(2)

    a   opp_diff
0   1   NaN
1   6   NaN
2   3   9.0
3   9   12.0
4   5   14.0
5   30  35.0
6   101 131.0
7   8   109.0

Generally to get an inverse of .diff(n) you should be able to do .cumsum().diff(n+1). The issue is that that you will get n+1 first results as NaNs

Upvotes: 0

Harshal Deore
Harshal Deore

Reputation: 1228

df.cumsum()

Example:
data = {'a':[1,6,3,9,5], 'b':[13,1,2,5,23]}
df = pd.DataFrame(data)

df = 
    a   b
0   1   13
1   6   1
2   3   2
3   9   5
4   5   23

df.diff()

a   b
0   NaN NaN
1   5.0 -12.0
2   -3.0    1.0
3   6.0 3.0
4   -4.0    18.0

df.cumsum()

a   b
0   1   13
1   7   14
2   10  16
3   19  21
4   24  44

Upvotes: 6

Scott Boston
Scott Boston

Reputation: 153460

You can use rolling with a window size of 2 and sum:

df['f'] = df['d'].rolling(2).sum().shift(-1)

    c  d    f
0  dd  1  3.0
1  ee  2  5.0
2  ff  3  7.0
3  gg  4  9.0
4  hh  5  NaN

Upvotes: 10

Related Questions