[英]Pandas unique values in two columns?
我對 pandas 很陌生。我有兩個與兩個玩家游戲相關的數據框
DF1:matches # match information
match_num winner_id loser_id points
270 201504 201595 28
271 201514 201426 19
272 201697 211901 21
273 201620 211539 30
274 214981 203564. 10
對於第270
場比賽, 201504 -> winner
和201595-> loser
各分享28
分。
我需要找出哪些玩家獲得的總分最高?
我正在使用 Hashmap 來解決這個問題?
hmap = defaultdict(int)
for index,row in matches_df.iterrows():
hmap[row["winner_id"]] += row["points"]
hmap[row["loser_id"]] += row["points"]
max_key = max(hmap, key=hmap.get)
這可以使用 pandas SQL 方式解決嗎?
用戶melt
堆疊兩個 id 列,然后 groupby:
(df[['winner_id','loser_id','points']]
.melt('points', value_name='id')
.groupby('id')['points'].sum()
)
Output:
id
201426.0 19
201504.0 28
201514.0 19
201595.0 28
201620.0 30
201697.0 21
203564.0 10
211539.0 30
211901.0 21
214981.0 10
Name: points, dtype: int64
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.