如何在 Excel 工作表中写入多个数据框

数据挖掘 熊猫 数据框 擅长 数据表
2021-10-07 04:52:40

我有多个具有相同列名的数据框。我想将它们一起写到一个垂直堆叠的excel表中。并且在每一个之间,会有一个占据一行的文本。这就是我的想法。

在此处输入图像描述

我尝试了 pandas.ExcelWriter() 方法,但每个数据帧都会覆盖工作表中的前一帧,而不是追加。

请注意,对于不同的数据框,我仍然需要多张工作表,而且每张工作表上还需要多个数据框。是否可以?或者任何其他可以从熊猫数据框动态生成excel表的python库?

4个回答

在同一张纸上写的一个例子:

import pandas as pd

data1 = """
class    precision   recall 
<18      0.0125         12           
18-24    0.0250         16 
25-34    0.00350         4
"""
data2 = """
class    precision   recall 
<18      0               0           
18-24    0.25            6 
25-34    0.35            5
"""

#create 2 df for sample
df1 = pd.read_csv(pd.compat.StringIO(data1), sep='\s+')
df1.name = "Dataframe1"
df2 = pd.read_csv(pd.compat.StringIO(data2), sep='\s+')
df2.name = "Dataframe2"
print(df1);print(df2)

writer = pd.ExcelWriter('e:\\test.xlsx',engine='xlsxwriter')
workbook=writer.book
worksheet=workbook.add_worksheet('Result')
writer.sheets['Result'] = worksheet
worksheet.write_string(0, 0, df1.name)

df1.to_excel(writer,sheet_name='Result',startrow=1 , startcol=0)
worksheet.write_string(df1.shape[0] + 4, 0, df2.name)
df2.to_excel(writer,sheet_name='Result',startrow=df1.shape[0] + 5, startcol=0)
writer.save()

输出:

在此处输入图像描述

如果你想写在不同的工作表中:

import pandas as pd

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('e:\\test.xlsx', engine='xlsxwriter')

# Write each dataframe to a different worksheet. you could write different string like above if you want
df1.to_excel(writer, sheet_name='Sheet1')
df2.to_excel(writer, sheet_name='Sheet2')

# Close the Pandas Excel writer and output the Excel file.
writer.save()

从这里这里使用选定的工作表将几个数据框发送到同一张工作表:

writer = pd.ExcelWriter('pandas_multiple.xlsx', engine='xlsxwriter')

# Position the dataframes in the worksheet.
df1.to_excel(writer, sheet_name='Sheet1')  # Default position, cell A1.
df2.to_excel(writer, sheet_name='Sheet1', startcol=3)
df3.to_excel(writer, sheet_name='Sheet1', startrow=6)

writer.save()

这是一个通用函数,用于将任意数量的 Pandas 数据帧写入单个 Excel 工作表:

import pandas as pd

def write_dataframes_to_excel_sheet(dataframes, dir, name):
    with pd.ExcelWriter(f'{dir}/{name}.xlsx', engine='xlsxwriter') as writer:
        workbook = writer.book
        worksheet = workbook.add_worksheet('Result')
        writer.sheets['Result'] = worksheet

        COLUMN = 0
        row = 0

        for df in dataframes:
            worksheet.write_string(row, COLUMN, df.name)
            row += 1
            df.to_excel(writer, sheet_name='Result',
                        startrow=row, startcol=COLUMN)
            row += df.shape[0] + 2

这是一个使用示例:

# Create sample dataframes
df1 = pd.DataFrame([(1, 2, 3), (4, 5, 6)], columns=('A', 'B', 'C'))
df1.name = "Dataframe1"
df2 = pd.DataFrame([(7, 8, 9), (10, 11, 12)], columns=('A', 'B', 'C'))
df2.name = "Dataframe2"
dataframes = (df1, df2)

write_dataframes_to_excel_sheet(dataframes, '/Users/foo/Documents', 'bar')

您可以打开excel编辑器并写入它然后保存

writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
data.to_excel(writer, sheet_name='Sheet1',
                  encoding='utf-8', index=False)
writer.save()

请参考这个答案https://stackoverflow.com/questions/34744863/python-how-to-use-excelwriter-to-write-into-an-existing-worksheet