Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort pandas MultiIndex

Tags:

python

pandas

I have created a Dataframe with a MultiIndex by using another Dataframe:

arrays = [df['bus_uid'], df['bus_type'], df['type'],
          df['obj_uid'], df['datetime']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['bus_uid', 'bus_type', 'type',
                                                 'obj_uid', 'datetime'])
multindex_df = pd.DataFrame(df['val'].values, index=index)

This worked fine as described in the documentation http://pandas.pydata.org/pandas-docs/stable/advanced.html .

In the documentation it also says that the labels need to be sorted for the correct working of indexing and slicing functionalities under "The need for sortedness with MultiIndex".

But somehow

multindexed_df.sort_index(level=0)

or

multindexed_df.sort_index(level='bus_uid')

does not work anymore and throws TypeError: sort_index() got an unexpected keyword argument 'level'.

Looking up the object information on sort_index() it looks as "by" is my new friend instead of "levels":

by:object
  Column name(s) in frame. Accepts a column name or a list for a nested sort. A tuple will be interpreted as the levels of a multi-index.

My question is the following: How can I sort my MultiIndex so that all functionalities (slicing,etc.) are working correctly?

like image 351
Cord Kaldemeyer Avatar asked Jan 04 '16 11:01

Cord Kaldemeyer


1 Answers

The answer depends on the pandas version you are working with. With the latest pandas (>= 0.17.0), you can indeed use the level keyword to specify to sort which level of the multi-index:

df = df.sort_index(level=0)

But, if you have an older pandas (< 0.17.0), this level keyword is not yet available, but you can use the sortlevel method:

df = df.sortlevel(level=0)

But note that if you want to sort all levels, you don't need to specify the level keyword, and you can just do:

df = df.sort_index()

This will work for both the recent and older versions of pandas.


For a summary of these changes in the sorting API, see http://pandas.pydata.org/pandas-docs/stable/whatsnew.html#changes-to-sorting-api

like image 160
joris Avatar answered Oct 30 '22 21:10

joris