1

I had a question that is borne out of data that is formatted in a way that isn't optimal to do "versus analysis". For example, we have one record per name per game, and we also have data for the player they played against (always 2 records per game; think center vs. center or pg vs. pg in basketball etc).

Current data:

    uid   name  field1  field2  field3
    1     bob       35      0       49
    1     evan       4      24      29
    2     bob       39      47      26
    2     mike       6      40      49
    3     bob       48      7       7
    3     evan      18      20      11
    4     bob        3      49      41
    4     evan      25      35      23

Desired Output:

uid     name    versus  field1  field2  field3
1        bob    evan    same as above 
1        evan   bob
2         bob   mike
2        mike   bob
3         bob   evan
3        evan   bob 
4         bob   evan    
4        evan   bob 

The goal of this re-arrangement/addition being that now I can do group-by analysis on one player vs. another over multiple games to find how they do on average in that matchup.

2 Answers 2

2

Since it is just 2 entries for each game, you could try grouping and reversing the names.

df['versus'] = df.groupby('uid')['name'].transform(lambda x: x[::-1])
Sign up to request clarification or add additional context in comments.

Comments

1

If you only need the other player's name, you should go for Rohith's solution. You can also use merge and query to get other players' statistics as well:

(df.merge(df, on='uid')
  .query('name_x != name_y')
)

Output

      uid  name_x      field1_x    field2_x    field3_x  name_y      field1_y    field2_y    field3_y
--  -----  --------  ----------  ----------  ----------  --------  ----------  ----------  ----------
 1      1  bob               35           0          49  evan               4          24          29
 2      1  evan               4          24          29  bob               35           0          49
 5      2  bob               39          47          26  mike               6          40          49
 6      2  mike               6          40          49  bob               39          47          26
 9      3  bob               48           7           7  evan              18          20          11
10      3  evan              18          20          11  bob               48           7           7
13      4  bob                3          49          41  evan              25          35          23
14      4  evan              25          35          23  bob                3          49          41

1 Comment

Thanks for this response as well. I did just need the player name but this is a great way to concatenate multiple row information into one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.