Home > Back-end >  Filter values from one dataframe based on conditional checks on another dataframe
Filter values from one dataframe based on conditional checks on another dataframe

Time:01-07

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 (e.g. 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

CodePudding user response:

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
  •  Tags:  
  • Related