pandas filtering using isin function

二次信任 提交于 2021-02-11 04:32:59

问题


I have two dataframe as shown below df1:

ID      Name
1       Sachin
2       Kholi
3       Dravid

df2:

ID      Run
1       20
2       60
2       10
1       5

From the above I want to filter df1 by only taking unique ids in df2:

Expected output:

ID      Name
3       Dravid

I tried below code

def diff(first, second):
        second = set(second)
        units_in_unit_table = [item for item in first if item not in second]
        return units_in_unit_table
id_df2 = diff(df2, df1)

df3 = df1[df1['ID'].isin(id_df2)]

回答1:


It seems your solution should be simplify by pass unique values to isin by Series.unique with invert mask by ~:

df3 = df1[~df1['ID'].isin(df2['ID'].unique())]

Or by set:

df3 = df1[~df1['ID'].isin(set(df2['ID']))]

print (df3)
   ID    Name
2   3  Dravid


来源:https://stackoverflow.com/questions/60183227/pandas-filtering-using-isin-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!