在一个具体问题上,假设我有一个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
Run Code Online (Sandbox Code Playgroud)
对于每个"单词",我想找到具有最多"计数"的"标签".所以回报就像是
word tag count
1 the S 20
2 a T 60
3 an T 5
Run Code Online (Sandbox Code Playgroud)
我不关心计数列,或者订单/索引是原始的还是搞砸了.返回字典{ 'the':'S',...}就好了.
我希望我能做到
DF.groupby(['word']).agg(lambda x: x['tag'][ x['count'].argmax() ] )
Run Code Online (Sandbox Code Playgroud)
但它不起作用.我无法访问列信息.
更抽象地说,agg(函数)中的函数看作什么?
顺便说一下,.agg()与.aggregate()相同吗?
非常感谢.