Issue
How can I get the differ between two pandas dataframe with the same dimensions:
import pandas as pd
df1 = pd.DataFrame({
'x': ['a', 'b', 'c', 'd', 'e'],
'y': [1, 1, 1, 1, 1],
'z': [2, 2, 2, 2, 2]})
print(df1)
df2 = pd.DataFrame({
'x': ['a', 'b', 'c', 'd', 'e'],
'y': [1, 1, 1, 1, 1],
'z': [3, 3, 3, 3, 3]})
print(df2)
I would like the output delta data frame is:
x y z
0 a 0 1
1 b 0 1
2 c 0 1
3 d 0 1
4 e 0 1
Solution
Set x
as the common index, subtract and reset the index (pandas aligns on the index before any operation):
df2.set_index('x').sub(df1.set_index('x')).reset_index()
x y z
0 a 0 1
1 b 0 1
2 c 0 1
3 d 0 1
4 e 0 1
Answered By - sammywemmy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.