Pandas 如何填写表中的索引
我使用pandas.pivot_表显示以下数据帧:Pandas 如何填写表中的索引,pandas,pivot-table,Pandas,Pivot Table,我使用pandas.pivot_表显示以下数据帧: A B a SH 1.0 b BJ 2.0 SH 3.0 c BJ 10.0 SH 5.0 因此,我想将数据帧更改为以下内容,我的意思是填充 索引中的空格: A B a SH 1.0 b BJ 2.0 b SH 3.0 c BJ 10.0 c SH 5.0 没有必要,只有pandas以这种方式显
A B
a SH 1.0
b BJ 2.0
SH 3.0
c BJ 10.0
SH 5.0
因此,我想将数据帧更改为以下内容,我的意思是填充
索引中的空格:
A B
a SH 1.0
b BJ 2.0
b SH 3.0
c BJ 10.0
c SH 5.0
没有必要,只有
pandas
以这种方式显示multi-index
您可以通过以下方式进行更改:
要写入excel,请将单元格连接在一起:
df.to_excel('file.xlsx')
可能的解决方案是,然后不通过index=False写入默认索引:
df.reset_index().to_excel('file.xlsx', index=False)
但我需要它将数据框导出到Excel,是否可以填充索引中的空白?检查编辑的答案。如果我的回答有帮助,别忘了。谢谢。很高兴能帮忙,周末愉快!
df.reset_index().to_excel('file.xlsx', index=False)