TypeError:输入类型不支持ufunc'invert'

时间:2020-04-09 10:34:28

标签: python pandas dataframe 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'''

我该如何解决?

1 个答案:

答案 0 :(得分:3)

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