Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pandas 如何填写表中的索引_Pandas_Pivot Table - Fatal编程技术网

Pandas 如何填写表中的索引

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以这种方式显

我使用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
以这种方式显示
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)