【问题标题】:TypeError: ufunc 'invert' not supported for the input typesTypeError:输入类型不支持 ufunc 'invert'
【发布时间】:2020-07-21 22:57:47
【问题描述】:

我正在尝试在我的数据框中删除行

栏目

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'''

我该如何解决??

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    因为如果长度大于7,则意味着大于8 解决方案应该简化 - 如果长度小于或等于7,则获取所有行:

    df = df[df['Field:FacilityCode'].str.len()<=7]   
    

    但你的解决方案是可能的:

    df[~(df['Field:FacilityCode'].str.len()>8) | ~(df['Field:FacilityCode'].str.len()>7)]
    

    编辑:从输出数据条件是&gt;7 过滤器等于或更高,如7

    df = df[df['Field:FacilityCode'].str.len()>=7]
    
    print (df)
      Field:FacilityCode
    0            mama100
    1           mimba190
    3            mimba67
    

    【讨论】:

    • 我试图通过添加更多细节来使我的问题更清楚
    • df[df['Field:FacilityCode'].str.len()&gt;=7] 失败?
    • 谢谢..我没有删除 ~ df = df[~df['Field:FacilityCode'].str.len()&lt;=7] 它有效
    猜你喜欢
    • 2018-03-09
    • 2020-09-29
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 2020-02-16
    • 2021-12-01
    • 2019-11-04
    相关资源
    最近更新 更多