【问题标题】:Pandas: TypeError: unsupported operand type(s) for |: 'str' and 'str' with more complex selection熊猫:TypeError:不支持的操作数类型|:'str'和'str'具有更复杂的选择
【发布时间】:2022-01-31 23:10:54
【问题描述】:

我试图根据第一个数据帧的选择标准构建一个新的数据帧。

df_selected = df_in.loc[ (df_in['featureclass'] == 'P' | df_in['featureclass'] == 'A') &
                         (df_in['admin1code'] == 'TX')
                   ]

返回此错误:

TypeError: |: 'str' 和 'str' 的操作数类型不受支持

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    需要将每个布尔值包装在它自己的一组括号中。

    df_selected = df_in.loc[ ((df_in['featureclass'] == 'P') | (df_in['featureclass'] == 'A')) &
                             (df_in['admin1code'] == 'TX')
                           ]
    

    【讨论】:

    • 这不是 Pandas 特有的。它涉及 Python 中的Operator precedence。因为== 是在| 之后评估的。
    猜你喜欢
    • 2017-08-24
    • 1970-01-01
    • 2013-02-20
    • 2018-12-06
    • 1970-01-01
    • 2021-01-03
    • 2018-04-22
    • 1970-01-01
    相关资源
    最近更新 更多