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

Question:

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:

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!

Asked By: Sean William

||

Answers:

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
Answered By: mozway

about last part :

df2 = df.apply(lambda x: x.iloc[0] - x)
Answered By: YfNk
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.