The Series.str.contains method expects a regular expression pattern (default), not a literal string. Therefore, str.contains("^") matches the beginning of any line. Since each line has a beginning, everything matches. Instead, use str.contains("\^") to match the alphabetic character ^ .
To check each column, you can use for col in df to iterate over the column names, and then call str.contains for each column:
mask = np.column_stack([df[col].str.contains(r"\^", na=False) for col in df]) df.loc[mask.any(axis=1)]
Alternatively, you can pass regex=False to str.contains so that the test uses the Python in statement; but (in general) using regex is faster.
unutbu
source share