简体   繁体   English

pandas group_by dataframe 写入 excel 时只输出聚合列; 如何在 excel 上获得整个 output?

[英]pandas group_by dataframe outputs only the aggregation column when written to excel; how to get the entire output on excel?

I am trying to group and sum-aggregate a specific column in my dataframe and then write this entire output to excel;我正在尝试对 dataframe 中的特定列进行分组和汇总,然后将整个 output 写入 excel; however, when i check the excel file after using the below code, it only contains the one aggregated column as the output and does not include any of the other grouping.但是,当我在使用以下代码后检查 excel 文件时,它仅包含一个聚合列作为 output 并且不包括任何其他分组。 I am someone could help me correct the code or provide suggestions as to how to achieve this?我有人可以帮助我更正代码或提供有关如何实现此目标的建议吗? Thanks in advance!提前致谢!

my_df = pd.DataFrame(df.groupby(['Parent Category','Expense'])['Parent Category','Expense','Variance'].sum())

ipynb 上的输出如下所示

Next, I use the below code to write it to excel but it does not write the output into excel as in the image above接下来,我使用下面的代码将其写入 excel 但它不会将 output 写入 excel,如上图所示

writer = pd.ExcelWriter('Test.xlsx', engine = 'xlsxwriter')
my_df.to_excel(writer,sheet_name = '1', index = False)
writer.save()

Excel 输出

why is the group by output not the same when written to excel?为什么output的组写入excel时不一样?

Can you please try the following?您可以尝试以下方法吗?

writer = pd.ExcelWriter('Test.xlsx', engine = 'xlsxwriter')
my_df.reset_index().to_excel(writer,sheet_name = '1', index = False)
writer.save()

Or if want to keep multi-index:或者如果想保留多索引:

writer = pd.ExcelWriter('Test.xlsx', engine = 'xlsxwriter')
my_df.to_excel(writer,sheet_name = '1', index = True)
writer.save()

暂无
暂无

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

相关问题 如何获取 pandas dataframe 中标题的“Excel”列名称? - How to get "Excel" column name for headers in pandas dataframe? Dataframe pandas 创建的维度检查以及如何删除 group_by function 创建的列? - Dataframe dimension check created by pandas and how to remove the column created by group_by function? 如何按 pandas dataframe 列分组并将不同的组保存到同一个 excel 文件中的多个工作表? - How to groupby a pandas dataframe column and save different group to multiple sheets in same excel file? 如何通过聚合摆脱熊猫中嵌套列的名称? - How to get rid of nested column names in Pandas from group by aggregation? 将包含 Excel 通用数字的 pandas dataframe 列转换为日期时,当单元格为空时收到错误消息 - while Converting pandas dataframe column containing Excel general numbers into date, get an error message when the cell is empty 如何在整个Pandas数据帧中搜索字符串,并获取包含该字符串的列的名称? - How to search entire Pandas dataframe for a string and get the name of the column that contains it? 如何从Excel获取行,然后将其作为列放入Pandas数据框中? - How do I get row from Excel and then put it into a Pandas dataframe as a column? 将 for 循环的 Dataframe 输出写入 Excel 而不覆盖 Pandas - Write Dataframe outputs from a for loop to Excel without overwriting Pandas 通过xlwings将数据从pandas中的数据帧写入Excel时如何跳过默认索引(左侧的系列列)? - How to skip default index (series column on the left) when writing data from dataframe in pandas into Excel through xlwings? 如何撤消熊猫数据框上的列聚合 - How to undo column aggregation on pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM