Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

opposite of df.diff() in pandas

Tags:

python

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.

like image 818
MissBleu Avatar asked Jan 24 '18 17:01

MissBleu


People also ask

What is the opposite of diff Python?

The cumulative sum method has in fact the opposite effect of the . diff() method that you came across in chapter 1.

What does diff () do in pandas?

The diff() method returns a DataFrame with the difference between the values for each row and, by default, the previous row. Which row to compare with can be specified with the periods parameter.

What does diff () do in Python?

diff() is used to find the first discrete difference of objects over the given axis.

How do you find the difference between two columns in pandas?

Difference of two columns in pandas dataframe in Python is carried out by using following methods : Method #1 : Using ” -” operator. Method #2 : Using sub() method of the Dataframe.


1 Answers

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
like image 119
Scott Boston Avatar answered Oct 06 '22 18:10

Scott Boston