假设我有一个如下数据框
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"]的形式的多索引。
我将如何进行?
答案 0 :(得分:0)
如果我理解正确:
您可以使用map
在字典中查找值(使用字典理解来交换键/值对,因为它们是向后的):
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