There are df1 with 10k values ​​and df2 with 15k values. They have only one common id column. It is necessary to trim df2 so that it contains only such id values ​​that are contained in df1 . Confused with ilocs and np.where'ami.

    1 answer 1

    The most idiomatic solution would be to use the Series.isin (other) method .

    Example:

     In [7]: df1 Out[7]: id ab 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 In [8]: df2 Out[8]: id cd 0 1 11 11 1 3 33 33 2 5 55 55 3 7 77 77 4 9 99 99 In [9]: df2['id'].isin(df1['id']) Out[9]: 0 True 1 True 2 False 3 False 4 False Name: id, dtype: bool In [10]: df2[df2['id'].isin(df1['id'])] Out[10]: id cd 0 1 11 11 1 3 33 33