【问题标题】: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')
]