我有以下df:
import numpy as np
import pandas as pd
from pandas import *
arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux']),
np.array(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'])]
s = pd.Series(np.random.randn(8), index=arrays)
df = pd.DataFrame(np.random.randn(8, 4), index=arrays)
这看起来像:
0 1 2 3
bar one -0.986089 -0.501170 1.635501 -0.789489
two 1.890491 -0.022640 -1.649097 0.984925
baz one -0.759930 -1.640487 -0.763909 -0.554997
two 1.636005 0.037158 0.567383 0.770314
foo one 0.709847 0.048332 -0.676660 1.059454
two 0.588063 0.568405 1.619102 0.393631
qux one -0.735497 -0.589282 1.015266 0.934877
two -0.380719 0.822213 0.295152 -0.838549
我想要获得的是两个有两列代表索引:
0 1 2 3 col1 col2
bar one -0.986089 -0.501170 1.635501 -0.789489 bar one
two 1.890491 -0.022640 -1.649097 0.984925 bar two
baz one -0.759930 -1.640487 -0.763909 -0.554997 baz one
two 1.636005 0.037158 0.567383 0.770314 baz two
foo one 0.709847 0.048332 -0.676660 1.059454 foo one
two 0.588063 0.568405 1.619102 0.393631 foo two
qux one -0.735497 -0.589282 1.015266 0.934877 qux one
two -0.380719 0.822213 0.295152 -0.838549 qux two
如果我只有一个级别的索引,通常的代码就是:
df['col'] = df.index
如何在多级索引上执行此操作?
答案 0 :(得分:2)
如果您只想将不同级别推送到列中,可以像这样重置索引:
df = df.reset_index()
要获得您展示的内容,您可以使用get_level_values
访问每个级别的值,如下所示:
In [69]: df['col1'] = df.index.get_level_values(0)
In [70]: df['col2'] = df.index.get_level_values(1)
In [71]: df
Out[71]:
0 1 2 3 col1 col2
bar one 0.523779 0.391620 0.726137 0.025270 bar one
two 0.569344 2.199075 -1.280942 -0.703693 bar two
baz one 0.347541 -0.423759 -1.010009 -0.349585 baz one
two -0.894432 -0.335773 -0.550428 0.217038 baz two
foo one 0.688120 -1.123873 0.784451 0.482187 foo one
two 0.062910 -0.705614 0.205807 -0.723899 foo two
qux one -0.304601 0.130234 0.303403 1.348833 qux one
two -0.931551 0.655013 0.622796 -0.738110 qux two