首页 > 解决方案 > 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'''

我该如何解决?

标签: pythonpandasdataframe

解决方案


因为如果长度更大,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

推荐阅读