[英]Pandas dataframe issue: `reset_index` does not remove hierarchical index
I am trying to flatten a Pandas Dataframe MultiIndex so that there is only a single level index. 我试图压平Pandas Dataframe MultiIndex,以便只有一个级别的索引。 The usual solution based on any number of SE posts is to use the
df.reset_index
command, but that is just not fixing the problem. 基于任意数量的SE帖子的通常解决方案是使用
df.reset_index
命令,但这不是解决问题的方法。
I started out with an Xarray DataArray
and converted it to a dataframe. 我开始使用
Xarray DataArray
并将其转换为数据帧。 The original dataframe looked like this. 原始数据框看起来像这样。
results
simdata a_ss_yr attr attr1 attr2 attr3
run year
0 0 0 0 0 0 0
1 1 6 2 0 4
2 2 4 2 2 0
3 3 1 0 0 1
4 4 2 0 2 0
To flatten the index I used 为了压平我使用的索引
df.reset_index(drop=True)
This only accomplished this: 这只完成了这个:
run year results
simdata a_ss_yr attr attr1 attr2
0 0 0 0 0 0 0
1 0 1 1 6 2 0
2 0 2 2 4 2 2
3 0 3 3 1 0 0
4 0 4 4 2 0 2
I tried doing the df.reset_index()
option more than once, but this is still not flattening the index, and I want to get this to only a single level index. 我尝试多次执行
df.reset_index()
选项,但这仍然没有使索引变平,我想把它变成一个单级索引。
More specifically I need the "run" and "year" variables to go to the level 0 set of column names, and I need to remove the "result" heading entirely. 更具体地说,我需要“运行”和“年”变量转到0级列名称,我需要完全删除“结果”标题。
I have been reading the Pandas documentation, but it seems like doing this kind of surgery on the index is not really described. 我一直在阅读Pandas文档,但似乎没有真正描述对索引进行这种手术。 Does anyone have a sense of how to do this?
有没有人知道如何做到这一点?
Use first droplevel
for remove first level of MultiIndex
and then reset_index
: 使用第一个
droplevel
删除第一级MultiIndex
,然后使用reset_index
:
df.columns = df.columns.droplevel(0)
df = df.reset_index()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.