python - Pandas sorting MultiIndex after concatenate -


when create multi-indexed table in 1 shot, sortlevel() works expected. however, if concatenate multiple tables create same multi-indexed table, cannot sortlevel() anymore. full example below:

import pandas pd a=pd.dataframe({'country':'zimbabwe','name':'fred'}, index=[1]) b=pd.dataframe({'country':'albania','name':'jeff'}, index=[0]) not_working = pd.concat([a,b],keys=['second','first']) working = pd.dataframe({'country':['zimbabwe','albania'],'name':['fred','jeff']}, index=pd.multiindex.from_tuples([('second',1),('first',0)]))  not_working_sorted = not_working.sortlevel(0) working_sorted = working.sortlevel(0) 

i expect both of these produce:

           country  name first  0   albania  jeff second 1  zimbabwe  fred 

however, "working". knows doing wrong ? using pandas 0.19.2

try ?

working.sort_index() out[702]:             country  name first  0   albania  jeff second 1  zimbabwe  fred 

or more specific

working.sort_index(level=[0, 1], ascending=[true, false]) 

edit: multiple index labels show below.

not_working.index out[565]:  multiindex(levels=[['second', 'first'], [0, 1]],            labels=[[0, 1], [1, 0]]) working.index out[566]:  multiindex(levels=[['first', 'second'], [0, 1]],            labels=[[1, 0], [1, 0]]) 

so if want not_working sort work :

not_working.sort_index(level=[0, 1], ascending=[false, false]) out[567]:             country  name first  0   albania  jeff second 1  zimbabwe  fred 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -