温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python报错: 'list' object has no attribute 'shape'怎么办

发布时间:2020-07-17 09:34:03 来源:亿速云 阅读:924 作者:小猪 栏目:开发技术

小编这次要给大家分享的是python报错: 'list' object has no attribute 'shape'怎么办,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。

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])

错误提示如下:

python报错: 'list' object has no attribute 'shape'怎么办

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

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

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

看完这篇关于python报错: 'list' object has no attribute 'shape'怎么办的文章,如果觉得文章内容写得不错的话,可以把它分享出去给更多人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI