首页 > 解决方案 > 从整个数据框 pandas python 中过滤掉包含特定字符串的行

问题描述

我正在尝试过滤掉熊猫中包含特定字符(¬)的所有行。

注意:列约为 1000,因此不能在代码中使用列名。

试图

filtered = pd.loc[:(pd == '¬').any(1).idxmax()]

输出:“DataFrame”对象没有属性“str”

标签: pythonpandas

解决方案


尝试:

from pandas.api.types import is_string_dtype
import numpy as np
filtered=df.loc[np.array([df[col].str.contains("¬") for col in df.columns if is_string_dtype(df[col])]).any(axis=0)]

数据框在哪里df。您可以通过调整内部列表并df.columns相应地调整要遍历的列数(现在它将遍历所有字符串类型的列)。


推荐阅读