仅单个列出现Pandas数值运算错误,其余列运行正常
问题:Pandas列数值乘1.1时报错TypeError
原本正常运行的脚本,功能是将列中数值增加10%后生成新列,但处理DBP_AUS列时出错。已确认该列全为数字,无空格、Null/NaN或字母,但执行以下代码仍报错:
df['COST_AUS'] = df['DBP_AUS'].multiply(1.1).round()
报错回溯信息(中文翻译):
Traceback (most recent call last): 文件 "C:\Users\rober\OneDrive\Documents\Datafeed\datafeed\script.Auscomp.py", 第28行, 在 <module>中 df['COST_AUS'] = df['DBP_AUS'].multiply(1.1).round() ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 文件 "C:\Users\rober\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\series.py", 第6014行, 在 mul中 return self._flex_method( ^^^^^^^^^^^^^^^^^^ 文件 "C:\Users\rober\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\series.py", 第5942行, 在 _flex_method中 return op(self, other) ^^^^^^^^^^^^^^^ 文件 "C:\Users\rober\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\ops\common.py", 第76行, 在 new_method中 return method(self, other) ^^^^^^^^^^^^^^^^^^^ 文件 "C:\Users\rober\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\arraylike.py", 第202行, 在 __mul__中 return self._arith_method(other, operator.mul) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 文件 "C:\Users\rober\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\series.py", 第5819行, 在 _arith_method中 return base.IndexOpsMixin._arith_method(self, other, op) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 文件 "C:\Users\rober\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\base.py", 第1381行, 在 _arith_method中 result = ops.arithmetic_op(lvalues, rvalues, op) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 文件 "C:\Users\rober\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\ops\array_ops.py", 第285行, 在 arithmetic_op中 res_values = _na_arithmetic_op(left, right, op) # type: ignore[arg-type] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 文件 "C:\Users\rober\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\ops\array_ops.py", 第229行, 在 _na_arithmetic_op中 result = _masked_arith_op(left, right, op) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 文件 "C:\Users\rober\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\ops\array_ops.py", 第184行, 在 _masked_arith_op中 result[mask] = op(xrav[mask], y) ^^^^^^^^^^^^^^^^^ TypeError: 无法将序列与非整数类型'float'相乘
问题分析
报错核心是TypeError: 无法将序列与非整数类型'float'相乘,说明DBP_AUS列的元素并非直接的数值类型,而是嵌套的列表/序列结构——比如每个元素是[123]这种单元素列表,而非纯数字123。即使视觉上看是数字,数据类型的嵌套会导致乘法操作失败。
解决方法
1. 先排查列的元素结构
执行以下代码确认数据类型和元素格式:
# 查看列的整体数据类型 print(df['DBP_AUS'].dtype) # 逐个检查前5个元素的实际类型 for idx, item in enumerate(df['DBP_AUS'].head()): print(f"第{idx+1}个元素: 值={item}, 类型={type(item)}")
2. 处理嵌套序列元素
如果排查后确认是列表/元组嵌套,提取其中的数值转为浮点型:
import pandas as pd # 提取嵌套序列中的第一个元素并转为数值 df['DBP_AUS'] = df['DBP_AUS'].apply( lambda x: float(x[0]) if isinstance(x, (list, tuple)) else float(x) ) # 执行原乘法逻辑 df['COST_AUS'] = df['DBP_AUS'].multiply(1.1).round()
3. 兜底:强制转为数值类型
如果是字符串格式的数字(即使你说没有,可作为额外排查),用pd.to_numeric强制转换:
# 强制转为数值,无法转换的标记为NaN df['DBP_AUS'] = pd.to_numeric(df['DBP_AUS'], errors='coerce') # 检查是否有转换失败的行(若有则说明存在非数字内容) print("转换失败的行:") print(df[df['DBP_AUS'].isna()])
内容的提问来源于stack exchange,提问作者DjInsomnia
相关产品推荐
相关产品推荐

