通过匹配Pandas df中所有列中的子字符串来过滤所有行

时间:2019-06-20 11:48:43

标签: python pandas

我有一个数据框,我希望将与给定子字符串匹配的所有行写为列表。

df1 = pd.DataFrame({'col1': ["c1", "match", "c3", "c4", "c5", "c6","c7","c8","c9"],  
                'col2': [" ","a1","a2","Smatch6","a4"," ","a8"," ","a7"],  
                   'col3': ["Red", "Ted", "Pet", "match1", "Pmatch", "Rmatchd","sample","agadR","Padgadg"]
                    }) 

输出应作为字典的列表

Data_Dictionary ['data'] =的o / p

尝试了此代码,但未返回任何内容

resultdf = df1.filter(like='match', axis=0)

预期结果:resultdf

    col1     col2     col3
1   match    a1       Ted
3   c4       Smatch6  match1
5   c6                Rmatchd

2 个答案:

答案 0 :(得分:0)

您可以使用Series.str.containsDataFrame.apply创建布尔掩码,然后通过DataFrame.any检查每行至少一个True并通过boolean indexing进行过滤:< / p>

resultdf = df1[df1.apply(lambda x: x.str.contains('match', na=False)).any(axis=1)]

或在DataFrame.applymap中使用in

resultdf = df1[df1.applymap(lambda x: 'match' in str(x)).any(axis=1)]

print (resultdf)
    col1     col2     col3
1  match       a1      Ted
3     c4  Smatch6   match1
4     c5       a4   Pmatch
5     c6           Rmatchd

答案 1 :(得分:0)

矢量化方法

>>> df1[df1.sum(axis=1).str.contains('match')]

    col1     col2     col3
1  match       a1      Ted
3     c4  Smatch6   match1
4     c5       a4   Pmatch
5     c6           Rmatchd