Код: Выделить всё
In [1]: import pandas as pd
In [2]: multi_index = pd.MultiIndex.from_product(
...: [['CAN', 'USA'], ['total']],
...: names=['country', 'sex'])
In [3]: df = pd.DataFrame({'pop': [35, 318]}, index=multi_index)
In [4]: df
Out[4]:
pop
country sex
CAN total 35
USA total 318
Код: Выделить всё
In [5]: df = df.query('pop > 100')
In [6]: df
Out[6]:
pop
country sex
USA total 318
Код: Выделить всё
In [7]: df.index.levels[0]
Out[7]: Index([u'CAN', u'USA'], dtype='object')
Код: Выделить всё
In [8]: idx_names = df.index.names
In [9]: df = df.reset_index(drop=False)
In [10]: df = df.set_index(idx_names)
In [11]: df
Out[11]:
pop
country sex
USA total 318
In [12]: df.index.levels[0]
Out[12]: Index([u'USA'], dtype='object')
Подробнее здесь: https://stackoverflow.com/questions/287 ... -dataframe
Мобильная версия