簡體   English   中英

類型錯誤:列表索引必須是整數或切片,而不是 str

[英]Type Error: list indices must be integers or slices, not str

我的代碼有什么問題?繼續獲取

TypeError: list indices must be integers or slices, not str"*
ata2 = [{'one': 1, 'two': 2}, {'one': 5, 'two': 10, 'three': 20}]


pd.DataFrame(ata2,index=['a','b'],columns=['one','two','three'])

ata2['four']=ata2['one']* ata2['two']

如果您嘗試進行列乘,則需要將ata2設置為pd.DataFrame ,因此,您需要: ata2 = pd.DataFrame(ata2,index=['a','b'],columns=['one','two','three'])

否則, ata2仍然是list ,而不是DataFrame - 您只能使用int的索引訪問列表!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM