welshm
welshm

Reputation: 1

Find duplicates within a column and pull into new df

I have two dataframes with duplicates within one column. I need to extract the duplicates into a separate dataframe.

df1=
    col1     col2   col3   col4
    BLUE      .5     yes    5
    GREEN     .2     no     2
    PINK      .3     yes    3

df2=
    col1     col2   col3   col4
    RED       .9     yes    9
    GREEN     .2     yes    2
    BLUE      .7     yes    7

For example, although the col2 in the GREEN row is not the same, I need to extract it from df2 only and pull it into a new df.

But also working with thousands of rows for hoping for a way to do this in bulk.

Upvotes: 0

Views: 26

Answers (1)

Adam.Er8
Adam.Er8

Reputation: 13413

IIUC you simply need to use .merge on col1:

new_df = df2.merge(df1['col1'], on='col1')

Output:

    col1  col2 col3  col4
0  GREEN   0.2  yes     2
1   BLUE   0.7  yes     7

Upvotes: 1

Related Questions