In this article, we are going to see how to find the difference between two rows in Pandas.
Pandas DataFrame is a two-dimensional data structure in tabular form with labeled axes. During data analysis, one might need to compute the difference between two rows for comparison purposes. This can be done using pandas.DataFrame.diff() function. This function calculates the difference between two consecutive DataFrame elements.
Syntax: pandas.DataFrame.diff(periods=1, axis=0)
Parameters:
- periods: Represents periods to shift for computing difference, Integer type value. Default value is 1
- axis: Represents difference to be taken over rown or columns. Can take two values {0: rows, 1: columns}. Default value is 0
Returns: Returns DataFrame
Find the Difference Between Two Rows
To test this function we created a dummy DataFrame with 3 columns and 6 rows. Now, this diff function will find the difference of each row with its previous row as periods by default is 1. Hence the reason why values in the 0th indexed row are NaN.
Python3
# Importing Pandas Library import pandas as pd # Creating dummy DataFrame for testing df = pd.DataFrame({ 'a' : [ 1 , 2 , 3 , 4 , 5 , 6 ], 'b' : [ 8 , 18 , 27 , 20 , 33 , 49 ], 'c' : [ 2 , 24 , 6 , 16 , 20 , 52 ]}) # Printing DataFrame before applying diff function print (df) # Printing DataFrame after applying diff function print ( "Difference: " ) print (df.diff()) |
Output:
a b c
0 1 8 2
1 2 18 24
2 3 27 6
3 4 20 16
4 5 33 20
5 6 49 52
Difference:
a b c
0 NaN NaN NaN
1 1.0 10.0 22.0
2 1.0 9.0 -18.0
3 1.0 -7.0 10.0
4 1.0 13.0 4.0
5 1.0 16.0 32.0
Find Difference Based on Condition
Python3
# Importing Pandas Library import pandas as pd # Creating dummy DataFrame for testing df = pd.DataFrame({ 'a' : [ 1 , 2 , 3 , 4 , 5 , 6 ], 'b' : [ 8 , 18 , 27 , 20 , 33 , 49 ], 'c' : [ 2 , 24 , 6 , 16 , 20 , 52 ]}) # Printing DataFrame before applying diff function print (df) # Printing DataFrame after applying diff function print ( "Difference: " ) print (df.diff(periods = 2 )) |
Output:
a b c
0 1 8 2
1 2 18 24
2 3 27 6
3 4 20 16
4 5 33 20
5 6 49 52
Difference:
a b c
0 NaN NaN NaN
1 NaN NaN NaN
2 2.0 19.0 4.0
3 2.0 2.0 -8.0
4 2.0 6.0 14.0
5 2.0 29.0 36.0