Search code examples
pythonpandasmulti-indexcolumnsorting

Stop Pandas from sorting columns


I am trying to produce a report and then I run the below code


    import pandas as pd

    df = pd.read_excel("proposals2020.xlsx", sheet_name="Proposals")

    country_probability = df.groupby(["Country", "Probability"]).count()
    country_probability = country_probability.unstack()
    country_probability = country_probability.fillna("0")
    country_probability = country_probability.drop(country_probability.columns[4:], axis=1)
    country_probability = country_probability.drop(country_probability.columns[0], axis=1)
    country_probability = country_probability.astype(int)

    print(country_probability)

I get the below results:

             Quote Number           
Probability          High Low Medium
Country                             
Algeria                 3   1      9
Bahrain                 4   3      2
Egypt                   2   0      3
Iraq                    3   0      8
Jordan                  0   1      1
Lebanon                 0   1      0
Libya                   1   0      0
Morocco                 0   0      2
Pakistan                3  10     11
Qatar                   0   1      1
Saudi Arabia           16   8     19
Tunisia                 2   5      0
USA                     0   1      0

My question is how to stop pandas from sorting these columns alphabetically and keep the High, Medium, Low order...


Solution

  • DataFrame.reindex

    # if isinstance(df.columns, pd.MultiIndex)
    df = df.reindex(['High', 'Medium', 'Low'], axis=1, level=1) 
    

    If not MultiIndex in columns:

    # if isinstance(df.columns, pd.Index)
    df = df.reindex(['High', 'Medium', 'Low'], axis=1)
    

    We can also try pass sort = False in groupby:

    country_probability = df.groupby(["Country", "Probability"], sort=False).count()