Search code examples
pythonpython-3.xpandasdifference

Getting dataframe with the absoulute difference between other two


I have a dataframe like the following

    A   B   C
1   10  20  30
2   40  50  60
3   70  80  90

And another datraframe like the following:

    A   B   C
1   10  21  30
2   40  50  60
3   65  80  95

I need a dataframe like that:

    A   B   C
1   0   1   0
2   0   0   0
3   5   0   5

Showing the absolute diference between the original dataframes. How can I get it?


Solution

  • You can use df.sub() with df.abs():

    df1.sub(df2).abs()
    

        A   B   C
    1   0   1   0
    2   0   0   0
    3   5   0   5