1. 程式人生 > 程式設計 >python報錯: 'list' object has no attribute 'shape'的解決

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'],2003])
#或者也可以寫成下面這樣:
pd.DataFrame(pop,index=pd.Series([2001,2003]))

以上這篇python報錯: 'list' object has no attribute 'shape'的解決就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。