2 回答

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超8個(gè)贊
我們可以嘗試groupby+ rank,然后map
df['Level']=df.groupby('ID').Price.rank().map({1:'low',2:'high'})
df
Out[221]:
ID Type Price Level
0 A one 7 low
1 A two 8 high
2 B one 9 high
3 B two 6 low

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊
讓我們?cè)囋嘾uplicated:
df['Level'] = (df.sort_values('Price').duplicated(['ID'])
.map({True:'high', False:'low'})
)
輸出:
ID Type Price Level
0 A one 7 low
1 A two 8 high
2 B one 9 high
3 B two 6 low
添加回答
舉報(bào)