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

Compute the difference between a row value and the rest in a column pandas

I have the following dataframe:

import pandas as pd
df = pd.DataFrame([
    [1, 4],
    [4, 6],
    [8, 2],
    [0, 8]
], columns=['a', 'b'])

How can I compute the difference between the first row vs the rest in a column like this:

df['operation_on_a'] = pd.DataFrame([
    ['1-1'],
    ['1-4'],
    ['1-8'],
    ['1-0']
])
df['operation_on_b'] = pd.DataFrame([
    ['4-4'],
    ['4-6'],
    ['4-2'],
    ['4-8']
])

and obtain result as follows using pandas only:

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

enter image description here

In numpy I found there is the triu_indices approach but I need to do this on the whole data table to obtain information from other columns.

Could you show me a way to solve this? Thank you!

>Solution :

Use rsub:

out = df.rsub(df.iloc[0])

or:

out = df.iloc[0] - df

Output:

   a  b
0  0  0
1 -3 -2
2 -7  2
3  1 -4

If you also need the string representation of the operations:

df.iloc[0].astype(str)+'-'+df.astype(str)

output:

     a    b
0  1-1  4-4
1  1-4  4-6
2  1-8  4-2
3  1-0  4-8
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