python - Python TypeError:无法将系列转换为计算标准差时
问题描述
我想从头开始计算标准偏差。它给了我一个TypeError: cannot convert the series to <class 'float'>
错误。
我尝试diff_squared
使用列表进行投射,.tolist()
但它反而引发TypeError: unsupported operand type(s) for /: 'list' and 'int'
了。
import pandas as pd
from math import sqrt
df = pd.read_csv('C:/Users/User/Downloads/Admission_Predict.csv')
# Mean
sums = 0
for m in range(len(df)):
sums += df.iloc[m]
mean = sums / len(df)
# Square of difference of mean and each value
diff_squared = 0
for n in range(len(df)):
diff_squared += (df.iloc[n].tolist() - mean) ** 2
# Standard deviation
stdv = sqrt(diff_squared / ((len(df)) - 1))
完整回溯
Traceback (most recent call last):
File "C:\Users\User\PycharmProjects\algorithms\Fibonacci recursive.py", line 18, in <module>
stdv = sqrt(diff_squared / ((len(df)) - 1))
File "C:\Users\User\PycharmProjects\algorithms\venv\lib\site-packages\pandas\core\series.py", line 141, in wrapper
raise TypeError(f"cannot convert the series to {converter}")
TypeError: cannot convert the series to <class 'float'>
Process finished with exit code 1
解决方案
推荐阅读
- python - 在python的目标文件夹中保存名称包含/的文件
- android - 如何根据json数组长度在设计中添加动态复选框?
- python - 字段 'sno' 需要一个数字,但得到了 ''
- php - 多维键正在覆盖 PHP 中的现有代码
- python - Pythonista 3 将 sqllite3 数据库连接到表视图
- python - 将输出转换为 Pandas 数据框
- html - 自动将 URL 下载为 HTML 文件,而不是访问页面
- google-cloud-functions - 使用 Python 云函数访问 Gmail 和 Google 表格
- mysql - 将增量 id 添加到 mysql 结果
- php - 如何将用户名和电子邮件输入从注册页面传递到登录页面?