将多索引数据框写入Excel文件 [英] Writing a multi-index dataframe to Excel file

查看:469
本文介绍了将多索引数据框写入Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DataFrame MultiIndex踢了我的屁股.经过一段时间的努力,我能够使用此代码创建MutliIndex DataFrame

The DataFrame MultiIndex is kicking my butt. After struggling for quite a while, I was able to create a MutliIndex DataFrame with this code

columns = pd.MultiIndex.from_tuples([('Zip', ''),
('All Properties', 'Avg List Price'),('All Properties', 'Median List Price'),
('3 Bedroom', 'Avg List Price'),('3 Bedroom', 'Median List Price'),
('2 Bedroom', 'Avg List Price'),('2 Bedroom', 'Median List Price'),
('1 Bedroom', 'Avg List Price'),('1 Bedroom', 'Median List Price')])
data[0] = ['11111', 'Val1', 'Val2', 'Val3', 'Val4', 'Val5', 'Val6', 'Val7', 'Val8']
df = pd.DataFrame(data, columns=columns)

一切正常,直到我尝试将其写入Excel文件

Everything looks fine until I try to write it to an excel file

writer = pd.ExcelWriter('testData.xlsx', engine='openpyxl')
df.to_excel(writer, 'Sheet1')
writer.save()

当我打开excel文件时,这就是我得到的.

When I open the excel file this is what I get.

如果我取消合并Excel中的列,则所有数据都在那里.

If I unmerge the columns in Excel all the data is there.

这是我要创建的图像

Here's an image of what I'm trying to create

我猜测该问题与我创建多索引列的方式有关,但我无法弄清楚问题是什么.

I'm guessing that the problem has something to do with the way I'm creating the multi index columns, but I can't figure out what the problem is.

我在Mac上运行python 2.7. 感谢您的输入.

I'm running python 2.7 on a Mac. Thanks for any input.

推荐答案

此错误将在版本0.17.1中修复,或者您可以使用engine='xlsxwriter'

This was a bug that will be fixed in version 0.17.1, or you can use engine='xlsxwriter'

https://github.com/pydata/pandas/pull/11328

这篇关于将多索引数据框写入Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆