Consider these two dataframes (simplified example):
DF1
Name Age
0 Tom 20
1 nick 21
2 krish 19
3 jack 18
DF2
Name Age
0 krish 40
1 jack 18
2 Tom 50
3 Jim 21
Note that the indices for a Name appearing in both doesn't match (this is the case for my dataset).
I would like to select those rows from DF1 where the Name is in DF2 and Age for that person doesn't match the corresponding value in DF2. So the expected output is:
Name Age
0 Tom 20
2 krish 40
I can filter rows that match one condition (eg Name is in DF2), but I've not been able to figure out how to check both conditions together. Any ideas?
In: df1[df1['Name'].isin(df2['Name'].tolist())]
Out:
Name Age
0 Tom 20
2 krish 19
3 jack 18
You can use merge
before filter out your dataframe:
>>> df1.merge(df2, on='Name', how='left', suffixes=('', '2')) \
.query('Age != Age2')[df1.columns]
Name Age
0 Tom 20
1 krish 19
You do with update
then compared the value to see whether it being updated
dfTemp = df1.copy()
dfTemp = df1.set_index('Name').copy()
dfTemp.update(df2.set_index('Name'))
df1[df1.Age!=dfTemp.Age.values]
Out[405]:
Name Age
0 Tom 20
2 krish 19
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.