我有一个数据框,我希望将与给定子字符串匹配的所有行写为列表。
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
答案 0 :(得分:0)
您可以使用Series.str.contains
和DataFrame.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