错误:列表索引必须是整数或切片,而不是str

时间:2019-10-17 18:09:30

标签: python python-3.x pandas

我的数据框如下所示:

enter image description here

我有包含多个页面的Example.xlsx电子表格:

enter image description here

目标是遍历daraframe StateID列并与excel工作表名称匹配,如果匹配,则写入单元格B14premiumtest

wb = openpyxl.load_workbook(r'\\username\Python\Treaty Year Report\Example.xlsx')
ws = [sheet.title for sheet in wb.worksheets]
print(ws)


for index, row in df1.iterrows():
    if any(x in 'MTHLY-'+ row['StateID'] for x in ws): 
        ws['B14'] = row[2]

但是我得到一个错误:

  File "x:\Documents\Treaty Year Report\TreatyYearReport3", line 76, in <module>
    ws['B14'] = row[2]
TypeError: list indices must be integers or slices, not str

那为什么我不能在工作表中传递单元格?如果传递索引,如何指定特定的单元格?

0 个答案:

没有答案