The absolute difference between two pandas DataFrame columns can be calculated using the abs() function. Examples:
Synthetic data
To start, let's generate a DataFrame using synthetic data:
import pandas as pdimport numpy as npnp.random.seed(42)a = -100b = 100data = np.random.random_sample((15,3)) * (b-a) + adf = pd.DataFrame(data=data, columns=['A','B','C'])
The code displayed above will generate for example:
A B C0 -25.091976 90.142861 46.3987881 19.731697 -68.796272 -68.8010962 -88.383278 73.235229 20.2230023 41.614516 -95.883101 93.9819704 66.488528 -57.532178 -63.6350075 -63.319098 -39.151551 4.9512866 -13.610996 -41.754172 22.3705797 -72.101228 -41.571070 -26.7276318 -8.786003 57.035192 -60.0652449 2.846888 18.482914 -90.70991710 21.508970 -65.895175 -86.98968111 89.777107 93.126407 61.67947012 -39.077246 -80.465577 36.84660513 -11.969501 -75.592353 -0.96461814 -93.122296 81.864080 -48.244004
Using pandas absolute abs() function
To compute the absolute difference between two pandas DataFrame columns, you can use the abs() function, example:
df['A'].abs() - df['B'].abs()
This will return a new column with the absolute difference between the two columns.
0 -65.0508851 -49.0645752 15.1480483 -54.2685864 8.9563505 24.1675476 -28.1431767 30.5301588 -48.2491899 -15.63602610 -44.38620511 -3.34929912 -41.38833113 -63.62285214 11.258215dtype: float64
Note: to get the absolute of the difference between two pandas DataFrame columns, a solution is to do
( df['A'] - df['B'] ).abs()
This will then return a new column:
0 115.2348381 88.5279692 161.6185073 137.4976174 124.0207065 24.1675476 28.1431767 30.5301588 65.8211959 15.63602610 87.40414611 3.34929912 41.38833113 63.62285214 174.986376dtype: float64
Additional features
Save result in a new column
df['Abs Diff A & B'] = df['A'].abs() - df['B'].abs()
This will then create a new column in the dataframe df:
A B C Abs Diff A & B0 -25.091976 90.142861 46.398788 -65.0508851 19.731697 -68.796272 -68.801096 -49.0645752 -88.383278 73.235229 20.223002 15.1480483 41.614516 -95.883101 93.981970 -54.2685864 66.488528 -57.532178 -63.635007 8.9563505 -63.319098 -39.151551 4.951286 24.1675476 -13.610996 -41.754172 22.370579 -28.1431767 -72.101228 -41.571070 -26.727631 30.5301588 -8.786003 57.035192 -60.065244 -48.2491899 2.846888 18.482914 -90.709917 -15.63602610 21.508970 -65.895175 -86.989681 -44.38620511 89.777107 93.126407 61.679470 -3.34929912 -39.077246 -80.465577 36.846605 -41.38833113 -11.969501 -75.592353 -0.964618 -63.62285214 -93.122296 81.864080 -48.244004 11.258215
Round absolute values
It is also possible to round a dataframe column using round() function:
df['Abs Diff A & B'].round(2)
this will give:
0 -65.051 -49.062 15.153 -54.274 8.965 24.176 -28.147 30.538 -48.259 -15.6410 -44.3911 -3.3512 -41.3913 -63.6214 11.26Name: Abs Diff A & B, dtype: float64
References
| Links | Site |
|---|---|
| pandas.DataFrame.abs | pandas.pydata.org |
| How to get the absolute value of a dataframe column in pandas ? | moonbooks.org |
