2016-11-20 26 views
0

另一個數據幀的數據幀的價值觀我有df_1包含兩個數據幀:查找在Python

["TP","MP"] 

和df_2包含:

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

我想在df_1搜索在每個使用值它輸入df_2 並返回匹配的那些。在這種情況下,那兩個有TP,MP的條目。

我試過這樣的事情。

df_2.str.contains(df_1) 

回答

1

您需要爲df_1的每個元素單獨執行此操作。熊貓會幫助你:

df_1.apply(df_2.str.contains) 

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

這是所有組合的矩陣。你可以很漂亮:

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