【发布时间】:2020-01-24 22:25:06
【问题描述】:
我有一个数据框如下:
df = pd.DataFrame({'unique_id':['x','x' , 'y', 'y'], 'chk':[5, 6, -4, -5], 'score':[5.52363, 6.73939, 7.53637, 3.08375]})
我想按'unique_id' 列分组并在'score' 列中查找最大值并将最大值设置为1,其他设置为0。
我试过df['result'] = df.groupby('unique_id').apply(lambda x: [x.score.idxmax()])
但它给了我空值。我的预期输出是
unique_ID chk score result
x 5 5.52363 0
x 6 6.73939 1
y -4 7.53637 1
y -5 3.08375 0
【问题讨论】:
标签: python pandas group-by max