Having two dataframes with different indices but with a matching column, how can I calculate the difference between them?
For example, with
df1 = pd.DataFrame({ 'a': (188, 750, 1330, 1385, 188, 750, 1330, 1385),
'b': (51.12, 51.45, 74.49, 29.21, 39.98, 3.98, 14.46, 16.51),
'c': pd.Categorical(['R', 'R', 'R', 'R', 'F', 'F', 'F', 'F']) })
df1 = df1.set_index(['a'])
b c
a
188 51.12 R
750 51.45 R
1330 74.49 R
1385 29.21 R
188 39.98 F
750 3.98 F
1330 14.46 F
1385 16.51 F
df2 = pd.DataFrame({ 'x': (20, 50),
'c': pd.Categorical(['R', 'F']) })
df2 = df2.set_index(['c'])
x
c
R 20
F 50
I would like to take the difference of column b in df1 with x of df2 based on the condition that column c in df1 should match the index c on df2.
The result would be something like:
b c diff
a
188 51.12 R 31.12
750 51.45 R 31.45
1330 74.49 R 54.49
1385 29.21 R 9.21
188 39.98 F -10.02
750 3.98 F -46.02
1330 14.46 F -35.54
1385 16.51 F -33.49
You can use join or map:
df1['diff'] = df1['b'] - df1.join(df2, on='c')['x']
print (df1)
b c diff
a
188 51.12 R 31.12
750 51.45 R 31.45
1330 74.49 R 54.49
1385 29.21 R 9.21
188 39.98 F -10.02
750 3.98 F -46.02
1330 14.46 F -35.54
1385 16.51 F -33.49
Or:
df1['diff'] = df1['b'] - df1['c'].map(df2['x'])
print (df1)
b c diff
a
188 51.12 R 31.12
750 51.45 R 31.45
1330 74.49 R 54.49
1385 29.21 R 9.21
188 39.98 F -10.02
750 3.98 F -46.02
1330 14.46 F -35.54
1385 16.51 F -33.49
df1.assign(diff = df1['b'] - df1['c'].map(df2.squeeze()))
Output:
b c diff
a
188 51.12 R 31.12
750 51.45 R 31.45
1330 74.49 R 54.49
1385 29.21 R 9.21
188 39.98 F -10.02
750 3.98 F -46.02
1330 14.46 F -35.54
1385 16.51 F -33.49
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With