有没有更简单的方法来操作 pandas DataFrame 的索引进出列而无需 DataFrame.reset_index()?

Is there an easier way to manipulate the index of a pandas DataFrame into and out of columns without DataFrame.reset_index()?

正在寻找一些用列更改索引的简短方法。设置示例

import numpy as np, pandas as pd
n = 5
df = pd.DataFrame(np.arange(0, n ** 2).reshape((n, n)))
df.columns = [f'c{i}' for i in range(n)]
df = df.set_index(['c0', 'c1', 'c2'])
print(df)

          c3  c4
c0 c1 c2        
0  1  2    3   4
5  6  7    8   9
10 11 12  13  14
15 16 17  18  19
20 21 22  23  24

现在更改索引,对于简单的情况

# instead of this (long)
df.reset_index().set_index(['c3', 'c4'])

# I want something like this (shorter)
df.change_index(['c3', 'c4'])

以及更复杂的情况

# instead of this (long and verbose)
names = list(df.index.names) 
names[names.index('c1')] = 'c3'
df.reset_index().set_index(names)

# something like this
df.change_index('c1', 'c3')

添加索引

# instead of 
names = list(df.index.names)
names.append('c3')
df.reset_index().set_index(names)

# something like this
df.add_index('c3')

删除索引

# instead of 
names = list(df.index.names)
names.remove('c1')
df.reset_index().set_index(names)

# something like this
df.remove_index('c1')

您可以像这样创建自己的数据框方法。

from pandas.core.base import PandasObject

def change_index(df, index=None):
    if index:
        return df.reset_index(drop=True).set_index(index)
    return df

PandasObject.change_index = change_index

然后你可以在你的数据帧上调用这个方法

df.change_index(['c0', 'c3'])

       c0  c2  c4
c1 c3            
1  3    0   2   4
6  8    5   7   9
11 13  10  12  14
16 18  15  17  19
21 23  20  22  24