python - 从整个数据框 pandas python 中过滤掉包含特定字符串的行
问题描述
我正在尝试过滤掉熊猫中包含特定字符(¬)的所有行。
注意:列约为 1000,因此不能在代码中使用列名。
试图
filtered = pd.loc[:(pd == '¬').any(1).idxmax()]
输出:“DataFrame”对象没有属性“str”
解决方案
尝试:
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
相应地调整要遍历的列数(现在它将遍历所有字符串类型的列)。
推荐阅读
- python - 使用 pandas to_sql 获取导致错误的记录行/列
- go - 如何在 Hugo 中解析变量返回的路径字符串中的括号?
- php - 如何在 PHP 中调用 AWS API 的 GetCostAndUsage
- tensorflow - 在训练期间控制 keras.layer 的权重
- sql - 在 SQL 中查找两行之间的差异并为其分配一个数值(SQLite)
- kubernetes-pod - 无法在所有 POD 中使用介质作为内存挂载指定的限制大小 emptydir 卷(tmpfs)
- php - Twilio - 发送未正确呈现的 vCard
- laravel-5 - laravel 中的路由问题如何在路由中创建许多操作?
- c# - 如何根据asp.net核心中的项目列表过滤实体
- django - django 模拟一位作者与多本书籍的关系