Python 为每个组选择最大值
所以我有一个包含多列和id列的熊猫数据框Python 为每个组选择最大值,python,pandas,Python,Pandas,所以我有一个包含多列和id列的熊猫数据框 df = pd.DataFrame(np.random.randn(6,4), columns=list('ABCD')) df['id'] = ['CA', 'CA', 'CA', 'FL', 'FL', 'FL'] df['technique'] = ['one', 'two', 'three', 'one', 'two', 'three'] df 我想按id列分组并选择概率最高的行。所以它可能看起来像这样 id highest_prob
df = pd.DataFrame(np.random.randn(6,4), columns=list('ABCD'))
df['id'] = ['CA', 'CA', 'CA', 'FL', 'FL', 'FL']
df['technique'] = ['one', 'two', 'three', 'one', 'two', 'three']
df
我想按id列分组并选择概率最高的行。所以它可能看起来像这样
id highest_prob technique
CA B three
FL C one
我试过这样的方法,但那只能让我半途而废
df.groupby('id', as_index=False)[['A','B','C','D']].max()
有人对我如何获得想要的结果有什么建议吗设置
np.random.seed(0) # Add seed to reproduce results.
df = pd.DataFrame(np.random.randn(6,4), columns=list('ABCD'))
df['id'] = ['CA', 'CA', 'CA', 'FL', 'FL', 'FL']
df['technique'] = ['one', 'two', 'three', 'one', 'two', 'three']
您可以melt
,使用sort\u值进行排序
,并使用drop\u duplicates
删除重复项:
(df.melt(['id', 'technique'])
.sort_values(['id', 'value'], ascending=[True, False])
.drop_duplicates('id')
.drop('value', 1)
.reset_index(drop=True)
.rename({'variable': 'highest_prob'}, axis=1))
id technique highest_prob
0 CA one D
1 FL two A
另一种解决方案是使用
melt
和groupby
:
v = df.melt(['id', 'technique'])
(v.iloc[v.groupby('id').value.idxmax()]
.drop('value', 1)
.reset_index(drop=True)
.rename({'variable': 'highest_prob'}, axis=1))
id technique highest_prob
0 CA one D
1 FL two A