对 pandas 数据框进行分组和多索引

Grouping and Multiindexing a pandas dataframe

假设我有一个数据框如下

In [6]: df.head()
Out[6]: 
     regiment company      name  preTestScore  postTestScore
0  Nighthawks     1st    Miller             4             25
1  Nighthawks     1st  Jacobson            24             94
2  Nighthawks     2nd       Ali            31             57
3  Nighthawks     2nd    Milner             2             62
4    Dragoons     1st     Cooze             3             70

我有一个字典如下:

army = {'Majors' : 'Nighthawks', 'Captains' : 'Dragoons'}

我想要它并且应该有一个 ["army","company"] 形状的多索引。

我将如何进行?

如果我理解正确的话:

您可以使用 map 在字典中查找值(使用字典理解交换 key/value 对,因为它们是向后的):

army = {'Majors': 'Nighthawks', 'Captains': 'Dragoons'}

df.assign(army=df.regiment.map({k:v for v, k in army.items()})).set_index(['army', 'company'], drop=True)
                    regiment      name  preTestScore  postTestScore
army     company                                                   
Majors   1st      Nighthawks    Miller             4             25
         1st      Nighthawks  Jacobson            24             94
         2nd      Nighthawks       Ali            31             57
         2nd      Nighthawks    Milner             2             62
Captains 1st        Dragoons     Cooze             3             70