我有以下几行代码来读取和编写excel:
df = pd.read_excel(file_path, sheet_name, header=[0, 1])
df.to_excel(output_path, index=False)
当它尝试写入excel时,我得到以下错误:
NotImplementedError: Writing to Excel with MultiIndex columns and no index ('index'=False) is not yet implemented
我不知道为什么会发生这种情况,我在网上也找不到具体的答案。
请帮帮忙。
发布于 2020-08-11 03:58:03
这是因为您的数据帧中有多个索引。您可以使用reset_index()或删除level=1索引。
如果您不需要索引,那么可以插入一个列作为索引df.insert(0,df.index,inplace=False) #,如下所示
发布于 2020-09-10 22:59:53
无法将多索引列导出到Excel。可以将多个索引转换为单个索引,然后将其导出到excelc。
df = df.reset_index()
df.to_excel('file_name.xlsx', index=False)
发布于 2021-01-28 14:22:47
多索引列实际上可以导出到Excel。您只需设置index=True即可。
所以对于这个例子,解决方案变成了.
df = pd.read_excel(file_path, sheet_name, header=[0, 1])
df.to_excel(output_path, index=True)
注意:从Pandas版本1.2.0开始就是这样
https://stackoverflow.com/questions/57626096
复制相似问题