简体   繁体   中英

Check if a string in a one df exists in another df in pandas

I have 2 Dataframes (df1 and df2). One common column in both the dataframes are 'Conct' which is concatenation of multiple columns. Goal is to find if ANY string in df1 exists in df2.

I tried to use merge, isin.. But all look for exact match.

Example: Data in df1:

Conct
ABC_ IronMan _x_nmc
xyz

Data in df2:

Conct
OPT_ IronMan _b_efd
GGH

In this example i want to get only those rows in df2 which matches "IronMan" in df1

Assuming we can split the strings in df1 by '_' and if we are just looking for rows that contain ANY of these strings then

df2[df2['Conct'].apply(lambda x: any([any([string in x for string in row]) for row in df1['Conct'].str.split('_')]))]

However, this will also look for substrings in df1 such as 'x' which will likely be much too broad, in which case you should redefine your goal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM