2017-10-17 129 views
0

我有6個數據框(名爲af),我想導入到ms.excel,爲了使這更容易閱讀我想給每個數據框標籤,我嘗試工作with cell notation,但我得到的錯誤mesage,這裏是我的代碼:給熊貓數據框標題導出爲ex​​cel

writer = pd.ExcelWriter('Summary Data Request 8 weeks.xlsx') 
a.to_excel(writer, sheet_name='Sheet1', startrow=1) 
b.to_excel(writer, sheet_name='Sheet1', startcol=7) 
c.to_excel(writer, sheet_name='Sheet1', startrow=11) 
d.to_excel(writer, sheet_name='Sheet1', startrow=11, startcol=6) 
e.to_excel(writer, sheet_name='Sheet1', startrow=21) 
f.to_excel(writer, sheet_name='Sheet1', startrow=21, startcol=6) 
writer.write('A1', 'Retail Kelontong') 
writer.write('G1', 'Pulsa') 
writer.write('A11', 'Minimarket') 
writer.write('G11', 'Retail dengan barcode') 
writer.write('A21', 'Retail dengan barcode 2') 
writer.write('G21', 'Food and Beverage') 
writer.save() 

這裏的錯誤消息:

AttributeError       Traceback (most recent call last) 
<ipython-input-30-2354b95670c9> in <module>() 
     6 e.to_excel(writer, sheet_name='Sheet1', startrow=21) 
     7 f.to_excel(writer, sheet_name='Sheet1', startrow=21, startcol=6) 
----> 8 writer.write('A1', 'Retail Kelontong') 
     9 writer.write('G1', 'Pulsa') 
    10 writer.write('A11', 'Minimarket') 


AttributeError: '_XlsxWriter' object has no attribute 'write' 

我仍然不知道我做錯了什麼,我試圖改變作家Sheet1和錯誤消息是

name 'Sheet1' is not defined 

我沒有想法什麼嘗試

回答

2

的根據文檔,你必須使用紙張的write方法,而不是作家:

writer = pd.ExcelWriter('Summary Data Request 8 weeks.xlsx') 
a.to_excel(writer, sheet_name='Sheet1', startrow=1) 
b.to_excel(writer, sheet_name='Sheet1', startcol=7) 
c.to_excel(writer, sheet_name='Sheet1', startrow=11) 
d.to_excel(writer, sheet_name='Sheet1', startrow=11, startcol=6) 
e.to_excel(writer, sheet_name='Sheet1', startrow=21) 
f.to_excel(writer, sheet_name='Sheet1', startrow=21, startcol=6) 
# Get sheet1 
sheet1 = writer.sheets['Sheet1'] 
sheet1.write('A1', 'Retail Kelontong') 
sheet1.write('G1', 'Pulsa') 
sheet1.write('A11', 'Minimarket') 
sheet1.write('G11', 'Retail dengan barcode') 
sheet1.write('A21', 'Retail dengan barcode 2') 
sheet1.write('G21', 'Food and Beverage') 
writer.save() 
+0

謝謝,這就是作品 –