Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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_Python 3.x_Pandas_Pandas Groupby - Fatal编程技术网

Python 如何做一个;“多索引”;子句

Python 如何做一个;“多索引”;子句,python,python-3.x,pandas,pandas-groupby,Python,Python 3.x,Pandas,Pandas Groupby,首先,我想说我是python的初学者,但这里有一个数据框架: df = pd.DataFrame({'countingVariable': ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'], 'color': ['red', 'red', 'orange', 'yellow', 'yellow', 'orange', 'red', 'yellow', 'orange'], 'foods': ['apple', 'pepper', 'apple', 'ap

首先,我想说我是python的初学者,但这里有一个数据框架:

df = pd.DataFrame({'countingVariable': ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'], 'color': ['red', 'red', 'orange', 'yellow', 'yellow', 'orange', 'red', 'yellow', 'orange'], 'foods': ['apple', 'pepper', 'apple', 'apple', 'apple', 'pepper', 'pepper', 'apple', 'apple']})
b = df.groupby(['color', 'foods']).count().sort_values(['countingVariable', 'foods', 'color'], ascending = [False, False, False])
其中b看起来像这样:

               countingVariable
color  foods                   
yellow apple                  3
red    pepper                 2
orange apple                  2
       pepper                 1
red    apple                  1
但我希望它看起来像这样的输出:

               countingVariable
color  foods                   
yellow apple                  3
red    pepper                 2
       apple                  1
orange apple                  2
       pepper                 1

因此,程序将找到最高计数,然后将其与它所属的组的其他部分一起放在顶部,这很奇怪。您将初始输出显示为

print(b)
               countingVariable
color  foods                   
yellow apple                  3
red    pepper                 2
orange apple                  2
       pepper                 1
red    apple                  1
然而,当我使用你的精确代码时,我得到了不同的输出

df = pd.DataFrame({
  'countingVariable': ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'],
  'color': ['red', 'red', 'orange', 'yellow', 'yellow', 'orange', 'orange', 'yellow', 'orange'],
  'foods': ['apple', 'pepper', 'apple', 'apple', 'apple', 'pepper', 'pepper', 'apple', 'apple']
    })
b = df.groupby(['color', 'foods']).count().sort_values(['countingVariable', 'foods', 'color'],
               ascending = [False, False, False])

print(b)
               countingVariable
color  foods                   
yellow apple                  3
orange pepper                 2
       apple                  2
red    pepper                 1
       apple                  1
这似乎是您实际想要的输出

编辑


也许您发布的数据与您实际使用的数据有所不同?

这应该可以做到:

df.groupby(['color', 'foods']).count().sort_values('countingVariable', ascending=False)
输出:

               countingVariable
color  foods                   
yellow apple                  3
orange apple                  2
       pepper                 2
red    apple                  1
       pepper                 1

需要在第0级上
.reindex
,以获得您的排序(食物按最高计数,然后在食物中下降)。这是因为
pd.unique
保留了顺序

import pandas as pd

b = b.reindex(b.index.unique(level=0), level=0)
输出:
您在
数据框中错误指定了一种颜色。我更新了它以重现您的输入/输出。Hm。这很奇怪。“看起来你复制的东西不知怎么地把第三个到最后一个颜色从橙色改成了红色,但事实上,@ALollz纠正了你的帖子——请看。”。通过他的编辑,我得到了与您发布的相同的输出。
               countingVariable
color  foods                   
yellow apple                  3
red    pepper                 2
       apple                  1
orange apple                  2
       pepper                 1