Salman Baqri
Salman Baqri

Reputation: 109

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)

Upvotes: 0

Views: 1307

Answers (1)

John Zwinck
John Zwinck

Reputation: 249642

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

Upvotes: 1

Related Questions