Pandas DataFrame多列多条件过滤报错及实现需求求助
问题与解决方案
需求说明
需对包含Title、Colour、Left In Stock、Price (£)、Weight(g)列的数据集应用以下过滤规则:
- 保留Left In Stock > 10且Price (£) ≥ 10的行
- 排除Weight(g) > 700的行
- 仅排除Title包含"the raven"或"on the"且Colour为White的行(标题含关键词但颜色非白色的行需保留)
报错场景
执行以下过滤代码时触发类型错误:
df = df.loc[ (df['Left In Stock']>10) & (df['Price (£)']>=10) & (df['Weight(g)']<=700) & ~( df['Title'].str.contains('the raven|on the') & df['Colour']=='White' ) ]
错误日志
Traceback (most recent call last): File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\ops\array_ops.py", line 301, in na_logical_op result = op(x, y) TypeError: unsupported operand type(s) for &: 'bool' and 'str' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\A\PycharmProjects\pythonProject\AutoTrader.py", line 189, in <module> df = df.loc[(df['Left In Stock']>10) & (df['Price (£)']>=10) & (df['Weight(g)']<=700) & ~(df['Title'].str.contains('the raven|on the') & df['Colour']=='White')] File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\ops\common.py", line 70, in new_method return method(self, other) File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\arraylike.py", line 70, in __and__ return self._logical_method(other, operator.and_) File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\series.py", line 5634, in _logical_method res_values = ops.logical_op(lvalues, rvalues, op) File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\ops\array_ops.py", line 391, in logical_op res_values = na_logical_op(lvalues, rvalues, op) File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\ops\array_ops.py", line 308, in na_logical_op result = libops.vec_binop(x.ravel(), y.ravel(), op) File "pandas\_libs\ops.pyx", line 252, in pandas._libs.ops.vec_binop File "pandas\_libs\ops.pyx", line 245, in pandas._libs.ops.vec_binop TypeError: unsupported operand type(s) for &: 'bool' and 'str' Process finished with exit code 1
问题根源
df['Title'].str.contains()在Title列存在缺失值时会返回NaN,导致该布尔数组与df['Colour']=='White'的布尔数组进行&运算时,出现bool与NaN(本质为浮点类型)的类型不匹配错误。
修复代码
给str.contains()添加na=False参数,将缺失值对应的判断结果设为False,同时拆分条件提升可读性:
# 定义各过滤条件 cond_stock_price = (df['Left In Stock'] > 10) & (df['Price (£)'] >= 10) cond_weight = df['Weight(g)'] <= 700 cond_exclude = ~(df['Title'].str.contains('the raven|on the', na=False) & (df['Colour'] == 'White')) # 应用所有条件过滤 df = df.loc[cond_stock_price & cond_weight & cond_exclude]
逻辑验证
na=False处理Title列的缺失值,避免NaN引发类型错误- 仅当标题含指定关键词且颜色为白色时才排除该行,完全符合需求
- 数值条件逻辑保持不变,确保过滤结果符合预期
内容的提问来源于stack exchange,提问作者Aslan
相关产品推荐
相关产品推荐

