Jak usunąć poziomy z wieloindeksowanej ramki danych?

Na przykład mam:

In [1]: df = pd.DataFrame([8, 9],
                          index=pd.MultiIndex.from_tuples([(1, 1, 1),
                                                           (1, 3, 2)]),
                          columns=['A'])

In [2] df
Out[2]: 
       A
1 1 1  8
  3 2  9

Czy istnieje lepszy sposób na usunięcie ostatniego poziomu z indeksu niż ten:

In [3]: pd.DataFrame(df.values,
                     index=df.index.droplevel(2),
                     columns=df.columns)
Out[3]: 
     A
1 1  8
  3  9
Author: feetwet, 2013-06-13

2 answers

df.reset_index(level=2, drop=True)
Out[29]: 
     A
1 1  8
  3  9
 46
Author: Yariv,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-13 10:43:36

Nie musisz tworzyć nowej instancji DataFrame! Możesz zmodyfikować indeks:

In [11]: df.index = df.index.droplevel(2)

In [12]: df
Out[12]:
     A
1 1  8
  3  9
 39
Author: Andy Hayden,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-13 10:41:22