pandas 向 MultiIndex 添加级别,删除不丢失

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11264307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 15:45:28  来源:igfitidea点击:

Adding levels to MultiIndex, removing without losing

pythonpandas

提问by Arthur G

Let's assume I have a DataFramedf with a MultiIndex and it has the level L.

假设我有一个DataFrame带有 MultiIndex的df 并且它的级别为 L。

Is there a way to remove L from the index and add it again?

有没有办法从索引中删除 L 并再次添加它?

df = df.index.drop('L')removes L completely from the DataFrame ( unlike df= df.reset_index()which has a drop argument). I could of course do df = df.reset_index().set_index(everything_but_L, inplace=True).

df = df.index.drop('L')从 DataFrame 中完全删除 L (不像df= df.reset_index()它有一个 drop 参数)。我当然可以df = df.reset_index().set_index(everything_but_L, inplace=True)

Now, let us assume the index contains everything but L, and I want to add L. df.index.insert(0, df.L)doesn't work. Again, I could of course call df= df.reset_index().set_index(everything_including_L, inplace=True)but it doesn't feel right.

现在,让我们假设索引包含除 L 之外的所有内容,并且我想添加 L。 df.index.insert(0, df.L)不起作用。同样,我当然可以打电话,df= df.reset_index().set_index(everything_including_L, inplace=True)但感觉不对。

Why do I need this? Since indices need not be unique, it can occur that I want to add a new column so the index becomes unique. Dropping may be useful in situations where after splitting data one level of the index does not contain any information anymore (say my index is A,B and I operate on a df with A=x but I do not want to lose A which would occur with index.droplevel('A')).

为什么我需要这个?由于索引不必是唯一的,因此可能会出现我想添加一个新列以使索引变得唯一的情况。在拆分数据后,索引的一级不再包含任何信息的情况下,删除可能很有用(假设我的索引是 A、B 并且我对 A=x 的 df 进行操作,但我不想丢失会发生的 A与 index.droplevel('A'))。

回答by Arthur G

In the current version (0.17.1) it is possible to

在当前版本 (0.17.1) 中,可以

df.set_index(column_to_add, append=True, inplace=True)

and

df.reset_index(level=column_to_remove_from_index).

This comes along with a substantial speedup versus resetting n columns and then adding n+1 to the index.

与重置 n 列然后将 n+1 添加到索引相比,这带来了显着的加速。