aggと同じaggregateです。呼び出し可能は、の列(Seriesオブジェクト)をDataFrame一度に1つずつ渡されます。
idxmax最大数の行のインデックスラベルを収集するために使用できます。
idx = df.groupby('word')['count'].idxmax()
print(idx)
収量
word
a       2
an      3
the     1
Name: count
次に、を使用して、および列locの行を選択します。wordtag
print(df.loc[idx, ['word', 'tag']])
収量
  word tag
2    a   T
3   an   T
1  the   S
インデックスラベルidxmaxを返すことに注意してください。ラベルで行を選択するために使用できます。ただし、インデックスが一意でない場合、つまり、インデックスラベルが重複している行がある場合は、にリストされているラベルを持つすべての行が選択されます。と一緒に使用する場合は注意してくださいdf.locdf.locidxdf.index.is_uniqueTrueidxmaxdf.loc
または、を使用することもできますapply。applyの呼び出し可能オブジェクトには、すべての列へのアクセスを提供するサブDataFrameが渡されます。
import pandas as pd
df = pd.DataFrame({'word':'a the a an the'.split(),
                   'tag': list('SSTTT'),
                   'count': [30, 20, 60, 5, 10]})
print(df.groupby('word').apply(lambda subf: subf['tag'][subf['count'].idxmax()]))
収量
word
a       T
an      T
the     S
を使用するidxmaxと、特に大きなDataFrameの場合、loc通常はより高速になります。applyIPythonの%timeitの使用:
N = 10000
df = pd.DataFrame({'word':'a the a an the'.split()*N,
                   'tag': list('SSTTT')*N,
                   'count': [30, 20, 60, 5, 10]*N})
def using_apply(df):
    return (df.groupby('word').apply(lambda subf: subf['tag'][subf['count'].idxmax()]))
def using_idxmax_loc(df):
    idx = df.groupby('word')['count'].idxmax()
    return df.loc[idx, ['word', 'tag']]
In [22]: %timeit using_apply(df)
100 loops, best of 3: 7.68 ms per loop
In [23]: %timeit using_idxmax_loc(df)
100 loops, best of 3: 5.43 ms per loop
単語をタグにマッピングする辞書が必要な場合は、次のように使用できset_index
ますto_dict。
In [36]: df2 = df.loc[idx, ['word', 'tag']].set_index('word')
In [37]: df2
Out[37]: 
     tag
word    
a      T
an     T
the    S
In [38]: df2.to_dict()['tag']
Out[38]: {'a': 'T', 'an': 'T', 'the': 'S'}