重置列的MultiIndex级别

时间:2013-01-07 03:59:30

标签: python pandas dataframe

是否有更短的方法来删除列MultiIndex级别(在我的情况下,basic_amt),除了将其移调两次?

In [704]: test
Out[704]: 
           basic_amt               
Faculty          NSW  QLD  VIC  All
All                1    1    2    4
Full Time          0    1    0    1
Part Time          1    0    2    3

In [705]: test.reset_index(level=0, drop=True)
Out[705]: 
         basic_amt               
Faculty        NSW  QLD  VIC  All
0                1    1    2    4
1                0    1    0    1
2                1    0    2    3

In [711]: test.transpose().reset_index(level=0, drop=True).transpose()
Out[711]: 
Faculty    NSW  QLD  VIC  All
All          1    1    2    4
Full Time    0    1    0    1
Part Time    1    0    2    3

3 个答案:

答案 0 :(得分:21)

另一个解决方案是使用MultiIndex.droplevelrename_axispandas 0.18.0中的新内容):

import pandas as pd

cols = pd.MultiIndex.from_arrays([['basic_amt']*4,
                                     ['NSW','QLD','VIC','All']], 
                                     names = [None, 'Faculty'])
idx = pd.Index(['All', 'Full Time', 'Part Time'])

df = pd.DataFrame([(1,1,2,4),
                   (0,1,0,1),
                   (1,0,2,3)], index = idx, columns=cols)

print (df)
          basic_amt            
Faculty         NSW QLD VIC All
All               1   1   2   4
Full Time         0   1   0   1
Part Time         1   0   2   3

df.columns = df.columns.droplevel(0)
#pandas 0.18.0 and higher
df = df.rename_axis(None, axis=1)
#pandas bellow 0.18.0
#df.columns.name = None

print (df)
           NSW  QLD  VIC  All
All          1    1    2    4
Full Time    0    1    0    1
Part Time    1    0    2    3

print (df.columns)
Index(['NSW', 'QLD', 'VIC', 'All'], dtype='object')

如果需要两个列名使用list理解:

df.columns = ['_'.join(col) for col in df.columns]
print (df)
           basic_amt_NSW  basic_amt_QLD  basic_amt_VIC  basic_amt_All
All                    1              1              2              4
Full Time              0              1              0              1
Part Time              1              0              2              3

print (df.columns)
Index(['basic_amt_NSW', 'basic_amt_QLD', 'basic_amt_VIC', 'basic_amt_All'], dtype='object')

答案 1 :(得分:12)

如何简单地重新分配df.columns

levels = df.columns.levels
labels = df.columns.labels
df.columns = levels[1][labels[1]]

例如:

import pandas as pd

columns = pd.MultiIndex.from_arrays([['basic_amt']*4,
                                     ['NSW','QLD','VIC','All']])
index = pd.Index(['All', 'Full Time', 'Part Time'], name = 'Faculty')
df = pd.DataFrame([(1,1,2,4),
                   (0,01,0,1),
                   (1,0,2,3)])
df.columns = columns
df.index = index

在:

print(df)

           basic_amt               
                 NSW  QLD  VIC  All
Faculty                            
All                1    1    2    4
Full Time          0    1    0    1
Part Time          1    0    2    3

后:

levels = df.columns.levels
labels = df.columns.labels
df.columns = levels[1][labels[1]]
print(df)

           NSW  QLD  VIC  All
Faculty                      
All          1    1    2    4
Full Time    0    1    0    1
Part Time    1    0    2    3

答案 2 :(得分:6)

Zip级别

这是一个替代解决方案,它将级别拉到一起并用下划线连接它们。

从上面的答案中得出,当我找到这个答案时,这就是我想要做的。我以为即使它没有回答上述问题也会分享。

["_".join(pair) for pair in df.columns]

给出

['basic_amt_NSW', 'basic_amt_QLD', 'basic_amt_VIC', 'basic_amt_All']

只需将其设为列

即可
df.columns = ["_".join(pair) for pair in df.columns]

           basic_amt_NSW  basic_amt_QLD  basic_amt_VIC  basic_amt_All
Faculty                                                              
All                    1              1              2              4
Full Time              0              1              0              1
Part Time              1              0              2              3