从整个数据框熊猫python过滤出包含特定字符串的行

时间:2019-12-06 23:13:42

标签: python pandas

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

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

尝试

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

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

1 个答案:

答案 0 :(得分:0)

尝试:

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来限制要迭代的列数(现在它将迭代所有字符串类型的列)。