我从命令提示符处收到以下错误:
Traceback (most recent call last):
File "C:\Python27\Scripts\read_csv.py", line 14, in <module>
df3.to_excel(writer, sheet_name="Section 3")
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 1464, in to_ex
cel
startrow=startrow, startcol=startcol)
File "C:\Python27\lib\site-packages\pandas\io\excel.py", line 1517, in write_cells
for cell in cells:
File "C:\Python27\lib\site-packages\pandas\formats\format.py", line 1893, in get_formatted_cells
self._format_body()):
File "C:\Python27\lib\site-packages\pandas\formats\format.py", line 1864, in _format_hierarchical_rows
fill_value=True)
File "C:\Python27\lib\site-packages\pandas\indexes\base.py", line 1515, in take
na_value=self._na_value)
File "C:\Python27\lib\site-packages\pandas\indexes\base.py", line 1534, in _assert_take_fillable
taken = values.take(indices)
IndexError: cannot do a non-empty take from an empty axes.
Exception Exception: Exception('Exception caught in workbook destructor. Explicit close() may be required for workbook.',) in <bound method Workbook.__del__ of
<xlsxwriter.workbook.Workbook object at 0x024BF5B0>> ignored
我尝试做的是从3个不同的CSV文件中获取数据,并将每个文件写入我保存的Excel文件的相应选项卡中。目前的代码如下:
import pandas as pd
df1=pd.read_csv("File1.csv")
df2=pd.read_csv("File2.csv")
df3=pd.read_csv("File3.csv")
# Creates a Pandas Excel writer using XlsxWriter as the engine
writer = pd.ExcelWriter("Excel File", engine="xlsxwriter")
# Convert data frames to an XlsxWriter Excel Object
df1.to_excel(writer, sheet_name="Section 1")
df2.to_excel(writer, sheet_name="Section 2")
df3.to_excel(writer, sheet_name="Section 3")
# Close the Pandas Excel writer and output the Excel file.
writer.save()
writer.close()