简体   繁体   中英

Pandas unique values in two columns?

I am very new to pandas. I have two dataframes related to two player Game

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 

for match num 270 both players 201504 -> winner and 201595-> loser shared 28 points each.

I need to find out Which player(s) got the highest number of points overall?

I am using Hashmap to solve this problem?

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)

Is this can be solved using pandas SQL way?

User melt to stack the two id columns, then 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM