'Select rows from a Pandas DataFrame with exactly the same column values in another DataFrame

Say I have the first pandas DataFrame below:

    A      B      ID
0  22.0    male   12
1  38.0  female   34
2  26.0  female   44
3  35.0  female   04
4  35.0    male   78

The second pandas DataFrame is:

    C   D   ID
0  xx  xx   12
2  xx  xx   44
4  xx  xx   78

I want the output be like:

    A      B      ID
0  22.0    male   12
2  26.0  female   44
4  35.0    male   78

which I only want to select rows from the first DataFrame that has the same ID appeared in the second DataFrame.

What is the most efficient way to do this?



Solution 1:[1]

Just use isin:

>>> df1[df1['ID'].isin(df2['ID'])]
      A       B  ID
0  22.0    male  12
2  26.0  female  44
4  35.0    male  78

Or merge: (prefer isin)

>>> df1.merge(df2['ID'])
      A       B  ID
0  22.0    male  12
1  26.0  female  44
2  35.0    male  78

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Corralien