我正在努力寻找每所大学最受欢迎的专业。
以下是该表的示例:
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非常新!
答案 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
指定为新索引。
根据您的修改:要按Institution
和Major
进行分组,您可以指定多个列进行分组:
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