更新时间:2023-12-01 10:40:28
创建布尔值DataFrame
,并通过DataFrame.any
并按
Create boolean DataFrame
and check at least one True
per row by DataFrame.any
and filter by boolean indexing
:
df = df[df.eq('a').any(axis=1)]
print (df)
A B
0 a b
2 e a
详细信息:
print (df.eq('a'))
A B
0 True False
1 False False
2 False True
print(df.eq('a').any(axis=1))
0 True
1 False
2 True
dtype: bool
如果要检查substring
,请使用 str.contains
表示boolean DataFrame
:
If want check substring
s use str.contains
for boolean DataFrame
:
df = pd.DataFrame([['ad', 'b'], ['c', 'd'], ['e', 'asw']], columns=["A", "B"])
print (df)
A B
0 ad b
1 c d
2 e asw
df = df[df.apply(lambda x: x.str.contains('a')).any(axis=1)]
或 applymap
in
进行明智的检查:
df = df[df.applymap(lambda x: 'a' in x).any(axis=1)]
print (df)
A B
0 ad b
2 e asw