停止排序列中的熊猫

问题描述 投票:1回答:2

我正在尝试生成报告,然后运行以下代码


    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)

我得到以下结果:

             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

我的问题是如何阻止熊猫按字母顺序对这些列进行排序并保持高,中,低顺序...

python pandas multi-index columnsorting
2个回答
0
投票
df=dfcountry.sort_values(by=[1])

© www.soinside.com 2019 - 2024. All rights reserved.