Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a new dataframe with difference of every two rows in Pandas

I have a dataframe in Pandas like the above:

    A   B   C
0   1  10  43
1   2  12  34
2   1   9  57
3   2   7  47
4   1   6  30
5   2  10  31

What I would like to do is to calculate the differences of every two rows according to column A (essentially get the differences of all the other columns when A=1 - A=2). So, I would like to come up with something like this:

    B   C
0  -2   9
1   2   10
2  -4  -1

I know about the diff() function but it doesn't seem to do the thing I want. Is there a way?

like image 507
tzoukritzou Avatar asked Mar 07 '19 16:03

tzoukritzou


Video Answer


2 Answers

You can take the floor division of the index by 2 and use the result as a grouper, then take the first differences of the groups using DataFrame.diff():

df.groupby(df.index//2)['B','C'].diff(-1).dropna().reset_index(drop=True)

    B     C
0 -2.0   9.0
1  2.0  10.0
2 -4.0  -1.0
like image 96
yatu Avatar answered Nov 13 '22 13:11

yatu


You can index by A and subtract:

x = df[df['A'] == 1].reset_index(drop=True).drop('A', axis=1)
y = df[df['A'] == 2].reset_index(drop=True).drop('A', axis=1)

x - y
like image 37
busybear Avatar answered Nov 13 '22 14:11

busybear