python - TypeError:输入类型不支持 ufunc 'invert'
问题描述
我正在尝试在我的数据框中删除行
专栏
Field:FacilityCode
mama100
mimba190
mama1
mimba67
#delete invalid code from df
df = df[~df['Field:FacilityCode'].str.len()>8] | df[~df['Field:FacilityCode'].str.len()>7]
预期产出
Field:FacilityCode
mama100
mimba190
这是错误
TypeError: ufunc 'invert' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'''
我该如何解决?
解决方案
因为如果长度更大,7
则意味着更大,则8
解决方案应该简化-如果长度小于或等于,则获取所有行7
:
df = df[df['Field:FacilityCode'].str.len()<=7]
但是您的解决方案是可能的:
df[~(df['Field:FacilityCode'].str.len()>8) | ~(df['Field:FacilityCode'].str.len()>7)]
编辑:从输出数据条件是>7
过滤器等于或更高,如7
:
df = df[df['Field:FacilityCode'].str.len()>=7]
print (df)
Field:FacilityCode
0 mama100
1 mimba190
3 mimba67
推荐阅读
- c++ - 如何抑制未使用的(void **arg)参数?
- javascript - `xhr.getAllHeaders()` 在浏览器扩展上下文中缺少一些标题?
- python - 如何按未排序的列表对数据框进行分组?
- excel - 单击边缘浏览器中的按钮
- git - 在 VSCode 中安装 git
- spring-boot - 具有多种身份验证方法的 Web 安全设置
- java - 如何在 Spring Data Elasticsearch 中为时间访问器的 HashMap 提供字段类型
- react-native - 部署时反应导航本机网络路由不起作用
- php - 如何使用 PHP 将 n 位数字随机排列到数组中?
- objective-c - 如何制作 dataWithEPSInsideRect 向量而不是向量格式的位图?