pythonpandasdataframecolumnsorting

Multiple column sorting in multiindex dataframe


I have the following dataframe:

dic = {'US':{'Traffic':{'new':1415, 'repeat':670}, 'Sales':{'new':67068, 'repeat':105677}},
      'UK': {'Traffic':{'new':230, 'repeat':156}, 'Sales':{'new':4568, 'repeat':10738}}}
d1 = defaultdict(dict)
for k, v in dic.items():
    for k1, v1 in v.items():
        for k2, v2 in v1.items():
            d1[(k, k2)].update({k1: v2})

df = pd.DataFrame(d1)

df.insert(loc=0, column=('', 'Mode'), value=[0,5])
df.insert(loc=1, column=('', 'Symbol'), value=[2,1])
df.columns = df.columns.rename("Skateboard", level=0)
df.columns = df.columns.rename("Q3", level=1)

I want to sort the column-Mode Descending and column-Symbol Ascending. I have tried the following:

df.sort_values(by = ['Mode', 'Symbol'], ascending = [False, True])

Solution

  • Your indexing is incorrect, use:

    df.sort_values(by=[('', 'Mode'), ('', 'Symbol')], ascending=[False, True])
    

    Output:

    Skateboard                 US            UK       
    Q3         Mode Symbol    new  repeat   new repeat
    Sales         5      1  67068  105677  4568  10738
    Traffic       0      2   1415     670   230    156