从Pandas数据框中,根据其他列的分组和最大值返回特定的列值

时间:2019-04-19 16:40:23

标签: python-3.x pandas dataframe

给出以下代码:

# Import pandas library 
import pandas as pd 


# Data to lists. 
 data = [{'Student': 'Eric', 'Grade': 96, 'Class':'A'}, \
{'Student': 'Caden', 'Grade': 92, 'Class':'A'}, \
{'Student': 'Sam', 'Grade': 90, 'Class':'A'}, \
{'Student': 'Leon', 'Grade': 88, 'Class':'A'}, \
{'Student': 'Laura', 'Grade': 80, 'Class':'B'}, \
{'Student': 'Leann', 'Grade': 22, 'Class':'B'}, \
{'Student': 'Glen', 'Grade': 9, 'Class':'C'}, \
{'Student': 'Jack', 'Grade': 90, 'Class':'C'}, \
{'Student': 'Jill', 'Grade': 87, 'Class':'C'}, \
{'Student': 'Joe', 'Grade': 58, 'Class':'C'}, \
{'Student': 'Andrew', 'Grade': 48, 'Class':'D'}, \
{'Student': 'Travis', 'Grade': 39, 'Class':'E'}, \
{'Student': 'Henry', 'Grade': 23, 'Class':'E'}, \
{'Student': 'Chris', 'Grade': 19, 'Class':'E'}, \
{'Student': 'Jim', 'Grade': 1, 'Class':'E'}, \
{'Student': 'Sarah', 'Grade': 93, 'Class':'E'}, \
{'Student': 'Brit', 'Grade': 92, 'Class':'E'}, \
] 

# Creates DataFrame. 
 df = pd.DataFrame(data) 

 print(df.groupby('Class')['Grade'].nlargest(2))

从数据框中,我想返回每个班级中成绩最好的2个学生的名字。我想返回两个不同版本的结果。

版本1包含所有原始列:

enter image description here

并且,版本2仅返回名称:

enter image description here

输出(希望使用上述两个版本):

enter image description here

2 个答案:

答案 0 :(得分:2)

IIUC,您可以sort_values,然后将head应用于您的JLabels对象

groupby

[出]

df_new = df.sort_values(['Class', 'Grade'], ascending=[True, False]).groupby('Class').head(2)

如果您需要过滤版本2输出,只需使用:

  Class  Grade Student
0      A     96    Eric
1      A     92   Caden
4      B     80   Laura
5      B     22   Leann
7      C     90    Jack
8      C     87    Jill
10     D     48  Andrew
15     E     93   Sarah
16     E     92    Brit

答案 1 :(得分:1)

复制过程的另一个选项是:

df.loc[df.groupby('Class')['Grade'].nlargest(2).index.get_level_values(1)]

   Class  Grade Student
0      A     96    Eric
1      A     92   Caden
4      B     80   Laura
5      B     22   Leann
7      C     90    Jack
8      C     87    Jill
10     D     48  Andrew
15     E     93   Sarah
16     E     92    Brit