如何使用Pandas Python找到相同值的最高计数?

时间:2017-07-08 22:48:35

标签: python pandas dataframe

我正在努力寻找每所大学最受欢迎的专业。

以下是该表的示例:

Institution   Major_Name  Count  Major
School 1      Art           2    First
School 1      English      12    First
School 1      Math          7    First
School 1      Art           6    Second
School 1      English       4    Second
School 1      Math          3    Second

School 2      Art         9
School 2      English     4
School 2      Math       13

我希望最终结果看起来像其他行将消失:

Institution   Major_Name   Count   Major
School 1      English       12     First
School 1      Art            6     Second
School 2      Math          13

提前致谢。使用Pandas非常新!

1 个答案:

答案 0 :(得分:1)

您可以在groupby上执行Institution,然后应用max功能:

In [547]: df.groupby('Institution', as_index=False).max()
Out[547]: 
  Institution  Major  Count
0     School 1  Math     12
1     School 2  Math     13

as_index=False属性会阻止生成的GroupBy对象将Institution指定为新索引。

根据您的修改:要按InstitutionMajor进行分组,您可以指定多个列进行分组:

In [563]: df.fillna('').groupby(['Institution', 'Major'], as_index=False).max()
Out[563]: 
  Institution   Major Major_Name  Count
0     School1   First       Math     12
1     School1  Second       Math      6
2     School2               Math     13