python pandas, DF.groupby().agg(), column reference in agg()
问题 On a concrete problem, say I have a DataFrame DF word tag count 0 a S 30 1 the S 20 2 a T 60 3 an T 5 4 the T 10 I want to find, for every "word", the "tag" that has the most "count" . So the return would be something like word tag count 1 the S 20 2 a T 60 3 an T 5 I don't care about the count column or if the order/Index is original or messed up. Returning a dictionary { 'the' : 'S' , ...} is just fine. I hope I can do DF.groupby(['word']).agg(lambda x: x['tag'][ x['count'].argmax() ] ) but