Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get difference of columns in DataFrame Pandas?

I need to get a DataFrame with column difference of column that I choose (for example the last one)

I tried using df.diff(axis=1, periods=1) will count column difference with the next column. However, I want to get difference of columns with exactly one column (last one).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use DataFrame.sub for subtract by last column selected by DataFrame.iloc:

df1 = df.sub(df.iloc[:, -1], axis=0)

If need subtract by column selected by label:

df1 = df.sub(df['col'], axis=0)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading