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:
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!
CodePudding user response:
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
CodePudding user response:
about last part :
df2 = df.apply(lambda x: x.iloc[0] - x)