arrays - 将一个 numpy 字符串数组更改为浮点数,但由于 +: 'float' 和 'str' 的操作数类型不受支持,无法获得其平均值或总和
问题描述
这是我拥有的具有 dtype=object 的数组
>>> a
array([25.0, 0.0, 0.0, ..., 0.0, 83.3333, 0.0], dtype=object)
我尝试使用该astype
方法转换数组但出现错误:
>>> a.astype(float)
ValueError: could not convert string to float:
我尝试像这样显式更改每个元素的 dtype:
for i in range(len(a)):
try:
a[i]=np.float(a[i])
except:
print(a[i])
它工作正常,没有打印任何内容。
然后我astype
再次尝试了该方法
>>> a.astype(float)
ValueError: could not convert string to float:
解决方案
推荐阅读
- sockets - 如何减少通过多播发送的数据包的传输大小?
- html - react-select inputProps 未应用于 AsyncSelect
- javascript - 有没有办法独立处理为多个可观察对象发出的值,然后在所有可观察对象都完成时做一些事情?
- git - 在合并之前将远程 HEAD 重置为特定的提交
- laravel - 使用 vuejs 和 laravel 将嵌套数据存储在文件中
- bash - 将 Xterm 的输出重定向到日志文件
- python - 尝试使用 Python 请求从 url 下载电子表格时获取登录页面
- javascript - 如何使用 jQuery 过滤处理多个数据属性?
- java - 节点作为反向代理限制了 java 后端的并发性
- javascript - 跳到 JSON 数组中的下一个对象并填充输入框