简体   繁体   中英

Find values of data frame in another dataframe in python

I have two data frames df_1 contains:

["TP","MP"]

and df_2 contains:

["This is case 12389TP12098","12378MP899" is now resolved","12356DCT is pending"]

I want to use values in df_1 search it in each entry of df_2 and return those which matches. In this case,those two entries which have TP,MP.

I tried something like this.

df_2.str.contains(df_1)

You need to do it separately for each element of df_1. Pandas will help you:

df_1.apply(df_2.str.contains)

Out: 
       0      1      2
0   True  False  False
1  False   True  False

That's a matrix of all combinations. You can pretty it up:

matches = df_1.apply(df_2.str.contains)
matches.index = df_1
matches.columns = df_2
matches

Out: 
   This is case 12389TP12098 12378MP899 is now resolved 12356DCT is pending
TP                      True                      False               False
MP                     False                       True               False

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