Reputation: 319
I have got two identical dataframes in pandas/python (a and b) only values are different:
a:
date a1 a2 a3
01.01.2020 2 2 2
02.01.2020 3 3 3
03.01.2020 4 4 4
b:
date a1 a2 a3
01.01.2020 1 1 1
01.01.2020 2 2 2
01.01.2020 3 3 3
I need a - b and expect to see result c:
date a1 a2 a3
01.01.2020 1 1 1
01.01.2020 1 1 1
01.01.2020 1 1 1
a - b does not work and I cannot figure out how. Would you please help me? Thanks!
Upvotes: 1
Views: 2652
Reputation: 150745
You can set_index
:
new_df = (a.set_index('date') - b.set_index('date')).reset_index()
However, that only works if your dates are identical between the two dataframes and different within each.
In the other case (as shown in your sample data) you can do:
c = b.copy()
c.iloc[:,1:] = a.iloc[:, 1:] - b.iloc[:,1:]
Upvotes: 2
Reputation: 34056
Assuming that dates in both dataframes are identical,
you can use df.sub
:
df = a.set_index('date').sub(b.set_index('date')).reset_index()
Output:
date a1 a2 a3
0 01.01.2020 1 1 1
1 01.01.2020 1 1 1
2 01.01.2020 1 1 1
Upvotes: 0