欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

python报错: 'list' object has no attribute 'shape'的解决

时间:2021-08-04 08:33:06|栏目:Python代码|点击:

numpy.array可使用 shape。list不能使用shape。

可以使用np.array(list A)进行转换。

(array转list:array B B.tolist()即可)

补充知识:Pandas使用DataFrame出现错误:AttributeError: 'list' object has no attribute 'astype'

在使用Pandas的DataFrame时出现了错误:AttributeError: ‘list' object has no attribute 'astype'

代码入下:

import pandas as pd
pop = {'Nevada': {2001: 2.4, 2002: 2.9},
'Ohio': {2000: 1.5, 2001: 1.7, 2002: 3.6}}

pd.DataFrame(pop, index=[2001, 2002, 2003])

错误提示如下:

原因:可能是Pandas版本问题,语法格式不正确。

解决办法:将代码写成如下格式,再次运行,没有报错。

pd.DataFrame(pop,columns=['Nevada', 'Ohio'],index=[2001,2002,2003])
#或者也可以写成下面这样:
pd.DataFrame(pop,index=pd.Series([2001,2002,2003]))

上一篇:Python表示矩阵的方法分析

栏    目:Python代码

下一篇:python之消除前缀重命名的方法

本文标题:python报错: 'list' object has no attribute 'shape'的解决

本文地址:http://www.codeinn.net/misctech/164028.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有