Python-pandas: How can I compare columns from two dataframes

Viewed 31

I have a problem with comparing columns two dataframes.

def function():
    x = {'col1': ['first', 'second'], 'col2': [[1,4,5], [3,6]]}
    y = {'col1': ['X', 'Y', 'Z'], 'col2': [1, 2, 3]}
    df1 = pd.DataFrame(data=x)
    df2 = pd.DataFrame(data=y)

How can I check if values from df2['col2'] are in df1['col2']? I would like to get a a new dataframe which will looks like this

  col1  col2    new_col
0    X     1     first
1    Y     2     FALSE/ NaN (etc.)
2    Z     3     second

My problem is that I don't know how to compare single element, in real data it is domain_name, to list of domain_names.

1 Answers

You can explode the col2 column of df1 then map or merge this column to df2

    df2['new_col'] = df2['col2'].map(df1.explode('col2', ignore_index=True)
                                     .set_index('col2')['col1'])
# or
    df2['new_col'] = df2[['col2']].merge(df1.explode('col2', ignore_index=True), on='col2', how='left')['col1']
print(df2)

  col1  col2 new_col
0    X     1   first
1    Y     2     NaN
2    Z     3  second
Related