更新时间:2023-12-01 10:00:52
Series.str.contains
方法需要正则表达式模式(默认情况下),而不是文字字符串.因此 str.contains("^")
匹配任何字符串的开头.由于每个字符串都有开头,因此所有内容都匹配.而是使用 str.contains("^")
来匹配文字 ^
字符.
The Series.str.contains
method expects a regex pattern (by default), not a literal string. Therefore str.contains("^")
matches the beginning of any string. Since every string has a beginning, everything matches. Instead use str.contains("^")
to match the literal ^
character.
要检查每一列,您可以使用 for col in df
遍历列名,然后在每一列上调用 str.contains
:
To check every column, you could use for col in df
to iterate through the column names, and then call str.contains
on each column:
mask = np.column_stack([df[col].str.contains(r"^", na=False) for col in df])
df.loc[mask.any(axis=1)]
或者,您可以将 regex=False
传递给 str.contains
以使测试使用 Python in
运算符;但是(通常)使用正则表达式会更快.
Alternatively, you could pass regex=False
to str.contains
to make the test use the Python in
operator; but (in general) using regex is faster.