简体   繁体   中英

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

What is the issue with my code?Keep getting

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

If you are trying to do a column multiplication, you need to set the ata2 as a pd.DataFrame , so, you need this: ata2 = pd.DataFrame(ata2,index=['a','b'],columns=['one','two','three'])

Otherwise, ata2 is still a list , instead of a DataFrame - and you can only access a list by using the indices which are int !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM