简体   繁体   English

熊猫-将多个数据框写入单个Excel工作表

[英]Pandas - Write multiple dataframes to single excel sheet

I have a dataframe with 45 columns and 1000 rows. 我有一个包含45列和1000行的数据框。 My requirement is to create a single excel sheet with the top 2 values of each column and their percentages (suppose col 1 has the value 'python' present 500 times in it, the percentage should be 50) 我的要求是使用每列的前2个值及其百分比创建一个excel工作表(假设col 1的值“ python”在其中存在500次,百分比应为50)

I used: 我用了:

    writer = pd.ExcelWriter('abc.xlsx')
    df = pd.read_sql('select * from table limit 1000', <db connection sring>)
    column_list = df.columns.tolist()
    df.fillna("NULL", inplace = True)
    for obj in column_list:
        df1 = pd.DataFrame(df[obj].value_counts().nlargest(2)).to_excel(writer,sheet_name=obj
writer.save()

This writes the output in separate excel tabs of the same document. 这会将输出写在同一文档的单独的excel选项卡中。 I need them in a single sheet in the below format: 我需要它们以以下格式放在一张纸上:

Column Name          Value         Percentage
col1                 abc           50
col1                 def           30
col2                 123           40
col2                 456           30

.... ....

Let me know any other functions as well to get to this output. 让我知道任何其他函数也可以到达此输出。

The first thing that jumps out to me is that you are changing the sheet name each time, by saying sheet_name=obj If you get rid of that, that alone might fix your problem. 让我sheet_name=obj的第一件事是,您每次都通过说sheet_name=obj来更改工作表名称。如果您摆脱了这一点,那么仅此一项就可以解决您的问题。

If not, I would suggest concatenating the results into one large DataFrame and then writing that DataFrame to Excel. 如果没有,我建议将结果串联到一个大的DataFrame中,然后将该DataFrame写入Excel。

for obj in column_list:
    df = pd.DataFrame(df[obj].value_counts().nlargest(2))
    if df_master is None:
        df_master = df
    else:
        df_master = pd.concat([df_master,df])
df_master.to_excel("abc.xlsx")

Here's more information on stacking/concatenating dataframes in Pandas https://pandas.pydata.org/pandas-docs/stable/user_guide/merging.html 这是有关在Pandas中堆叠/连接数据帧的更多信息https://pandas.pydata.org/pandas-docs/stable/user_guide/merging.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编写多个熊猫数据框以表现出色 - Write multiple pandas dataframes to excel 如何将多个数据框写入一张 Excel 表格? - How do I write multiple dataframes to one excel sheet? 熊猫将多个数据框合并为一个 - Pandas multiple dataframes into single 将多个数据帧写入单个平面文本文件以输入 Pandas - Write multiple dataframes into a single flat text file to input into Pandas 如何将多个 Pandas 数据帧写入 Excel? (当前方法 Corrupts.xlsx) - How to Write Multiple Pandas Dataframes to Excel? (Current Method Corrupts .xlsx) 将熊猫数据框导出到Excel工作表中的不同行 - Export pandas dataframes to different rows in Excel sheet 使用 pandas xlsxwriter 写入 excel 表中的多行 - Write to multiple rows in excel sheet using pandas xlsxwriter 将数据框的(刮取的)列表写入单个 Excel 工作表 - Writing a (scraped) list of dataframes to single excel sheet 如何将数据帧的字典写到Pandas中的一个excel文件中? 键是工作表名称,值是数据框 - How to write a dict of dataframes to one excel file in Pandas? Key is sheet name, Value is the dataframe 有没有办法从单个 xlsx 读取多个 excel 选项卡/工作表到多个数据帧,每个 dataframe 以工作表名称命名? - is there a way to read multiple excel tab/sheets from single xlsx to multiple dataframes with each dataframe named with sheet name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM