我需要为每个 station_list 值创建一个 excel 表并在其中写入两个数据框
dfVentaPotencial = ventaPotencial()
print(dfVentaPotencial.head)
CodigoEstacion Dia 0am 1am 2am 3am 4am 5am 6am 7am 8am 9am 10am 11am 12pm 1pm 2pm 3pm 4pm 5pm 6pm 7pm 8pm 9pm 10pm 11pm
0 60008 1 4 42 0 5 4 41 56 176 0 22 0 649 0 182 227 118 176 135 0 150 112 235 118 17
1 60008 2 37 0 13 0 0 27 11 0 0 0 9 439 601 463 715 1027 706 617 172 0 0 192 151 22
2 60008 3 2 16 2 11 0 38 98 0 0 0 0 0 0 0 0 0 0 0 0 0 0 97 0 114
3 60008 4 23 9 12 7 2 0 0 73 0 0 0 0 0 0 0 0 0 0 510 457 0 60 0 49
4 60008 5 0 0 0 23 3 58 129 0 0 0 0 328 0 0 0 575 0 0 121 87 300 73 68 0
.. ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
925 60999 27 16 0 2 0 11 0 0 73 0 0 80 78 128 32 0 0 81 239 86 89 176 0 0 0
926 60999 28 0 0 6 0 36 64 0 0 0 0 0 109 365 0 113 0 0 0 48 21 149 0 0 0
927 60999 29 0 45 4 0 13 16 12 0 72 84 40 0 48 0 0 0 0 0 0 0 0 5 0 0
928 60999 30 0 2 8 5 11 0 0 95 0 0 0 63 92 0 2 30 0 0 270 0 0 34 0 0
929 60999 31 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
dfTrxAtendedor = trxAtendedores()
dfTrxAtendedor = pd.pivot_table(dfTrxAtendedor,values='trxXAtendedor',index=["CodigoEstacion","Dia"],columns="Hora",fill_value=0).reset_index()
print(dfTrxAtendedor.head)
Hora CodigoEstacion Dia 0am 1am 2am 3am 4am 5am 6am 7am 8am 9am 10am 11am 12pm 1pm 2pm 3pm 4pm 5pm 6pm 7pm 8pm 9pm 10pm 11pm
0 60008 1 4 0 0 0 0 0 4 6 10 8 16 10 21 15 18 18 14 13 16 12 12 7 7 8
1 60008 2 2 2 0 2 0 2 3 4 6 8 11 15 20 23 10 13 15 16 18 19 14 4 6 4
2 60008 3 0 0 0 0 1 6 9 14 10 14 15 12 13 15 12 13 12 16 18 19 15 7 7 4
3 60008 4 0 0 0 0 0 6 11 12 13 16 12 16 12 9 13 14 19 18 17 16 13 7 9 4
4 60008 5 2 2 0 0 0 4 4 12 15 13 14 12 13 14 11 10 15 18 19 17 15 10 8 19
.. ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
925 60999 27 1 2 0 1 0 3 7 10 22 14 11 14 4 8 12 18 14 16 18 14 15 14 8 2
926 60999 28 1 1 1 1 0 1 6 7 16 14 12 9 4 16 14 14 16 14 18 16 10 13 4 4
927 60999 29 3 0 0 1 0 1 2 3 6 12 12 14 14 11 15 14 7 13 13 11 10 6 5 3
928 60999 30 2 0 0 0 0 2 2 1 6 9 8 6 8 9 12 8 9 12 8 14 8 6 2 2
我一直在尝试这个循环,但我收到以下错误
estation_list = [60008, 60012,60021,60048,60050,60053,60055,60072,60074,60078,60079,60088,60096,60134,60135,60139,60265,60268,60371,60411,60512,60517,60610,60664,60773,60783,60814,60840,60890,60999]
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
for estation in estation_list:
dataframe_collection = {}
dataframe_collection[estation] = dfTrxAtendedor[dfTrxAtendedor["CodigoEstacion"] == estation]
dataframe_collection[estation] = dfVentaPotencial[dfVentaPotencial["CodigoEstacion"] == estation]
dataframe_collection.to_excel(writer, sheet_name=estation)
writer.save()
dataframe_collection.to_excel(writer, sheet_name=estation)
AttributeError: 'dict' object has no attribute 'to_excel'
我是 phyton 的新手我正在尝试这种方法,因为我需要格式化单元格并且使用 pandas 似乎不能,所以我打算根据 station_list 生成带有 n 个工作表的 excel 文件,然后使用 openpyxl 编辑这些文件和完成以 PDF 格式导出工作表