问题
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