2016-11-20 56 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