pandas:如何将多个索引合并在一起?

时间:2022-11-11 22:57:16

I have two levels of index, and I want to merge them into one level of index. I looked at methods such as reset_index and reindex, but they don't seem to be what I need. Another way I can think of is adding a new column containing the merged indexes, set that column as the new index using pivot_table, and delete the old indexes. But I'm wondering is there a more elegant way to do this. Any input is welcomed. Thank you so much!

我有两个级别的索引,我想将它们合并到一个索引级别。我查看了reset_index和reindex等方法,但它们似乎并不是我需要的。我能想到的另一种方法是添加一个包含合并索引的新列,使用pivot_table将该列设置为新索引,并删除旧索引。但我想知道是否有更优雅的方式来做到这一点。欢迎任何意见。非常感谢!

1 个解决方案

#1


Doing

df.index = df.index.values

will give you tuples in a single level if that's what you mean by 'merge'.

如果这就是'merge'的意思,你会在单一级别给你元组。

#1


Doing

df.index = df.index.values

will give you tuples in a single level if that's what you mean by 'merge'.

如果这就是'merge'的意思,你会在单一级别给你元组。