python - 无法获取 Pandas 数据框中数值列的平均值
问题描述
当我尝试在我的 Pandas 数据框中获取我的数字列的平均值时,我遇到了运行时错误。最初我认为它与 dtype 有关,但我检查并(如您所见)它们都是数字:
df = pd.read_csv(DATA_PATH, delimiter=',')
df.dtypes
id object
age int64
income float64
children int64
gender object
region object
married object
car object
savings_acct object
current_acct object
mortgage object
pep object
dtype: object
df.head(5)
id age income children gender region married car savings_acct current_acct mortgage pep
0 ID12101 48 17546.0 1 FEMALE INNER_CITY NO NO NO NO NO YES
1 ID12102 40 30085.1 3 MALE TOWN YES YES NO YES YES NO
2 ID12103 51 16575.4 0 FEMALE INNER_CITY YES YES YES YES NO NO
3 ID12104 23 20375.4 3 FEMALE TOWN YES NO NO YES NO NO
4 ID12105 57 50576.3 0 FEMALE RURAL YES NO YES NO NO NO
这是我遇到问题的地方:
# means
print("Means: \n")
print(df[["age","income","children"]].mean() + "\n")
Means:
---------------------------------------------------------------------------
UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U32'), dtype('<U32')) -> dtype('<U32')
During handling of the above exception, another exception occurred:
UFuncTypeError Traceback (most recent call last)
<ipython-input-27-e56207deaccf> in <module>
1 # means
2 print("Means: \n")
----> 3 print(df[["age","income","children"]].mean() + "\n")
~/anaconda3/lib/python3.7/site-packages/pandas/core/ops/common.py in new_method(self, other)
62 other = item_from_zerodim(other)
63
---> 64 return method(self, other)
65
66 return new_method
~/anaconda3/lib/python3.7/site-packages/pandas/core/ops/__init__.py in wrapper(left, right)
498 lvalues = extract_array(left, extract_numpy=True)
499 rvalues = extract_array(right, extract_numpy=True)
--> 500 result = arithmetic_op(lvalues, rvalues, op, str_rep)
501
502 return _construct_result(left, result, index=left.index, name=res_name)
~/anaconda3/lib/python3.7/site-packages/pandas/core/ops/array_ops.py in arithmetic_op(left, right, op, str_rep)
195 else:
196 with np.errstate(all="ignore"):
--> 197 res_values = na_arithmetic_op(lvalues, rvalues, op, str_rep)
198
199 return res_values
~/anaconda3/lib/python3.7/site-packages/pandas/core/ops/array_ops.py in na_arithmetic_op(left, right, op, str_rep)
149 result = expressions.evaluate(op, str_rep, left, right)
150 except TypeError:
--> 151 result = masked_arith_op(left, right, op)
152
153 return missing.dispatch_fill_zeros(op, left, right, result)
~/anaconda3/lib/python3.7/site-packages/pandas/core/ops/array_ops.py in masked_arith_op(x, y, op)
110 if mask.any():
111 with np.errstate(all="ignore"):
--> 112 result[mask] = op(xrav[mask], y)
113
114 result, _ = maybe_upcast_putmask(result, ~mask, np.nan)
UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U32'), dtype('<U32')) -> dtype('<U32')
我也尝试将数字类型全部转换为浮点数,以便它们保持一致,但它(不出所料)没有任何区别:/
解决方案
推荐阅读
- node.js - 启动 npm 时出错:web 应用程序的根目录中不存在 node_modules
- linux - 使用 shell 脚本替换文件中的多行文本
- java - Spring Data JPA Query:根据来自另一张表的真/假值总量预填充/更新一个表
- python - 期望 str 在添加零时发现浮点错误
- linq - 是否支持实体框架 Linq 语句(带有子查询)?
- testng - 在 TestNG 框架的情况下,Report Portal 如何截取失败的测试用例?
- apache-spark - Kubernetes 上的 Spark-submit,即使在 spark 作业完成后,executor pod 仍在运行。由于这些资源不能免费用于新工作
- c - C 错误:此处不允许函数定义,我已经将函数放在 int main(void) 之上,删除了括号,错误仍然存在
- reactjs - React useReducer 中的问题
- ruby - Ruby/Watir 禁用保存密码提示