本文介绍了将许多python大 pandas 数据框放在一个excel工作表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只要不同的工作表,很容易将许多熊猫数据框添加到excel工作簿中。但是,如果要使用大熊猫内置的df.to_excel功能,将许多数据帧变成一个工作表是有点棘手。从Pandas
writer = pd.ExcelWriter('test.xlsx',engine =')创建Excel Writer对象

It is quite easy to add many pandas dataframes into excel work book as long as it is different worksheets. But, it is somewhat tricky to get many dataframes into one worksheet if you want to use pandas built-in df.to_excel functionality.

# Creating Excel Writer Object from Pandas
writer = pd.ExcelWriter('test.xlsx',engine='xlsxwriter')
workbook=writer.book
worksheet=workbook.add_worksheet('Validation')
df.to_excel(writer,sheet_name='Validation',startrow=0 , startcol=0)
another_df.to_excel(writer,sheet_name='Validation',startrow=20, startcol=0)

以上代码将无法正常工作。您将收到错误

The above code won't work. You will get the error of

 Sheetname 'Validation', with case ignored, is already in use.

现在,我已经尝试了足够的,我找到了一种方法使其工作。

Now, I have experimented enough that I found a way to make it work.

writer = pd.ExcelWriter('test.xlsx',engine='xlsxwriter')   # Creating Excel Writer Object from Pandas
workbook=writer.book
df.to_excel(writer,sheet_name='Validation',startrow=0 , startcol=0)
another_df.to_excel(writer,sheet_name='Validation',startrow=20, startcol=0)

这将工作。所以,我在stackoverflow上发布这个问题的目的是双重的。首先,如果他/她正试图将许多数据框放在Excel中的单个工作表中,我希望这将有助于他人。

This will work. So, my purpose of posting this question on stackoverflow is twofold. Firstly, I hope this will help someone if he/she is trying to put many dataframes into a single work sheet at excel.

其次,有人可以帮助我了解这两个代码块之间的区别吗?在我看来,它们几乎是一样的,除了第一个代码创建的工作块,提前称为验证,而第二个不是。我得到那部分

Secondly, Can someone help me understand the difference between those two blocks of code? It appears to me that they are pretty much the same except the first block of code created worksheet called "Validation" in advance while the second does not. I get that part.

我不明白是为什么会有什么不同?即使我不提前创建工作表,这行,最后一行的行,

What I don't understand is why should it be any different ? Even if I don't create the worksheet in advance, this line, the line right before the last one,

 df.to_excel(writer,sheet_name='Validation',startrow=0 , startcol=0)

将创建一个工作表无论如何。因此,当我们到达最后一行代码时,工作表验证也已经在第二个代码块中创建。所以,我的问题基本上,为什么第二个代码块工作的时候第一个不行?

will create a worksheet anyway. Consequently, by the time we reached the last line of code the worksheet "Validation" is already created as well in the second block of code. So, my question basically, why should the second block of code work while the first doesn't?

如果还有另一种方法可以使用内置的df.to_excel功能将许多数据框放入excel中,请分享!

Please also share if there is another way to put many dataframes into excel using the built-in df.to_excel functionality !!

推荐答案

user3817518:如果还有另一种方法可以使用内置df.to_excel将许多数据框放入excel功能!!

user3817518: "Please also share if there is another way to put many dataframes into excel using the built-in df.to_excel functionality !!"

这是我的尝试:

简单的方法是将很多数据框放在一个工作表或跨多个选项卡。让我知道这是否有效!

Easy way to put together a lot of dataframes on just one sheet or across multiple tabs. Let me know if this works!

- 要测试,只需运行示例数据框和代码的第二和第三部分。

-- To test, just run the sample dataframes and the second and third portion of code.

import pandas as pd
import numpy as np

# Sample dataframes
randn = np.random.randn
df = pd.DataFrame(randn(15, 20))
df1 = pd.DataFrame(randn(10, 5))
df2 = pd.DataFrame(randn(5, 10))



将多个数据帧放入一个xlsx表单



Put multiple dataframes into one xlsx sheet

# funtion
def multiple_dfs(df_list, sheets, file_name, spaces):
    writer = pd.ExcelWriter(file_name,engine='xlsxwriter')
    row = 0
    for dataframe in df_list:
        dataframe.to_excel(writer,sheet_name=sheets,startrow=row , startcol=0)
        row = row + len(dataframe.index) + spaces + 1
    writer.save()

# list of dataframes
dfs = [df,df1,df2]

# run function
multiple_dfs(dfs, 'Validation', 'test1.xlsx', 1)



将多个数据框放在不同的选项卡/ h3>

Put multiple dataframes across separate tabs/sheets

# function
def dfs_tabs(df_list, sheet_list, file_name):
    writer = pd.ExcelWriter(file_name,engine='xlsxwriter')
    for dataframe, sheet in zip(df_list, sheet_list):
        dataframe.to_excel(writer, sheet_name=sheet, startrow=0 , startcol=0)
    writer.save()

# list of dataframes and sheet names
dfs = [df, df1, df2]
sheets = ['df','df1','df2']

# run function
dfs_tabs(dfs, sheets, 'multi-test.xlsx')

这篇关于将许多python大 pandas 数据框放在一个excel工作表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 07:04