python報(bào)錯(cuò): 'list' object has no attribute 'shape'的解決
numpy.array可使用 shape。list不能使用shape。
可以使用np.array(list A)進(jìn)行轉(zhuǎn)換。
(array轉(zhuǎn)list:array B B.tolist()即可)
補(bǔ)充知識(shí):Pandas使用DataFrame出現(xiàn)錯(cuò)誤:AttributeError: ’list’ object has no attribute ’astype’
在使用Pandas的DataFrame時(shí)出現(xiàn)了錯(cuò)誤:AttributeError: ‘list’ object has no attribute ’astype’
代碼入下:
import pandas as pdpop = {’Nevada’: {2001: 2.4, 2002: 2.9},’Ohio’: {2000: 1.5, 2001: 1.7, 2002: 3.6}}pd.DataFrame(pop, index=[2001, 2002, 2003])
錯(cuò)誤提示如下:
原因:可能是Pandas版本問(wèn)題,語(yǔ)法格式不正確。
解決辦法:將代碼寫(xiě)成如下格式,再次運(yùn)行,沒(méi)有報(bào)錯(cuò)。
pd.DataFrame(pop,columns=[’Nevada’, ’Ohio’],index=[2001,2002,2003])#或者也可以寫(xiě)成下面這樣:pd.DataFrame(pop,index=pd.Series([2001,2002,2003]))
以上這篇python報(bào)錯(cuò): ’list’ object has no attribute ’shape’的解決就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
