以类似于 R 中 dplyr 中的组函数的方式解释 Pandas 中的分组

Explaining Grouping in Pandas in a way similar to the group function in dyplr in R

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

为什么当我们按 varaibel 对熊猫 df 进行分组,然后对结果进行排序时,为什么我们看不到分组的 rosw togather,就像 R 中的组函数 dplyr 中的情况一样?

例如,我有这个数据框:

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

如果我们按 item 分组,然后按 Price 排序,我们应该看到“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_values:

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。