Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.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 以类似于dyplr中的group函数的方式解释熊猫中的分组_Python_R_Pandas_Pandas Groupby - Fatal编程技术网

Python 以类似于dyplr中的group函数的方式解释熊猫中的分组

Python 以类似于dyplr中的group函数的方式解释熊猫中的分组,python,r,pandas,pandas-groupby,Python,R,Pandas,Pandas Groupby,我找不到答案的简单问题: 为什么当我们用一个varaibel将一个panda df分组,然后对结果进行排序时,为什么我们不能像R中的分组函数dplyr那样看到分组的rosw呢 例如,我有以下数据框: Item Type Price A 1 22 B 1 58 C 1 33 A 2 80 A 3 50 B 2

我找不到答案的简单问题:

为什么当我们用一个varaibel将一个panda df分组,然后对结果进行排序时,为什么我们不能像R中的分组函数dplyr那样看到分组的rosw呢

例如,我有以下数据框:

Item      Type    Price 
 A         1       22
 B         1       58
 C         1       33
 A         2       80
 A         3       50
 B         2       98
 C         3       63
 B         5        8
如果我们按
项目
分组,然后按
价格
排序,我们应该看到“A”在一起,“B”在一起,两个“C”在一起,这三个组中的每一个都被排序。我们如何在python中实现这一点

我试过这个:

df.groupby('Item').sort_values(['Price']) # This is not right becuase we can not access the sort function on the grouped by object

df.sort_values('Price').groupby(['Item']) # This does part of the job, but I wnder why I can not see the groupped items togather? 
预期输出如下所示:

Item      Type    Price 
 A         2       80 
 A         3       50    
 A         1       22
 B         2       98
 B         1       58
 B         5        8
 C         3       63
 C         1       33

要获得输出,可以使用
df.sort\u值

In [783]: df.sort_values(['Item', 'Price'],  ascending=[True, False])
Out[783]: 
  Item  Type  Price
3    A     2     80
4    A     3     50
0    A     1     22
5    B     2     98
1    B     1     58
7    B     5      8
6    C     3     63
2    C     1     33

不需要groupby。

您能给我们一个预期输出的示例吗?我很困惑,因为您似乎可以互换使用“排序”和“分组”。您需要用数据说明所有要点,正如前面的评论所要求的。大多数熊猫人可能不知道dplyr的
groupby
是什么样子。就这一点而言,他们可能不精通R。不,我不是交替使用他们,我是分组然后排序。按“x”分组,按“y”排序。我想将输出作为一个数据帧,按项目分组,按价格排序。@您尝试过哪些不起作用?你们是说按项目分组(聚合),然后按价格排序吗?还是按项目排序,然后按价格排序?这是我们完全不清楚的地方。我编辑了我的问题,并添加了我尝试的内容