Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 停止对列进行排序_Python_Pandas_Multi Index_Columnsorting - Fatal编程技术网

Python 停止对列进行排序

Python 停止对列进行排序,python,pandas,multi-index,columnsorting,Python,Pandas,Multi Index,Columnsorting,我正在尝试生成一个报告,然后运行下面的代码 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()

我正在尝试生成一个报告,然后运行下面的代码


    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
我的问题是如何阻止熊猫按字母顺序排列这些列,并保持高、中、低顺序

如果不是列中的多索引:

# if isinstance(df.columns, pd.Index)
df = df.reindex(['High', 'Medium', 'Low'], axis=1)
我们还可以在
groupby
中尝试passsort=False

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

是这样的吗?试试df.sort_值(['High','Medium','Low'])Groupby的变量sort默认设置为True,您可以传递sort=false谢谢Vaishali。。这对代码进行了一些调整..不客气!你能考虑接受这个答案吗?)对不起,伙计,我的第一个问题是:)
country_probability = df.groupby(["Country", "Probability"], sort=False).count()