带有预定义行特定顺序的Python .describe()函数

假设我有以下测试DataFrame df:

Car      Sold      make       profit 

Honda     100      Accord      10
Honda      20      Fit          5
Toyota    300      Corolla     20
Hyundai   150      Elantra     20
BMW        20      Z-class    100 
Toyota     45      Lexus        7
BMW        50      X-class     30
JEEP      150      cherokee     2
Honda      20      CRV          5
Toyota     30      Yaris        3

我需要一个汇总统计表,用于按汽车类型划分的售出汽车数量。

我可以这样:

df.groupby('Car')['Sold'].describe()

这给了我类似以下内容:

Car      count      mean       std     min    25th   50th   75th    max  

BMW        2       
Honda      3        
Hyundai    1  
JEEP       1   
Toyota     3      

The 'Car' column values are listed in the summary statistic table in alphabetically ascending order. I am looking for a way to sort it in my own pre-specified way. I want the summary statistic table to be listed as "Toyota, Hyundai, JEEP, BMW, Honda"

评论
  • iut
    iut 回复
    df.groupby('Car')['Sold'].describe().loc[["Toyota", "Hyundai", "JEEP", "BMW", "Honda"]]
    

    helps me put it in order, but I am not able to do it for multi-level indexing. For instance, if I want the summary statistics table by 'Car', and further by the make, .loc does not give me the desired solution.