0

I have the following df -

    +--------+--------+--------------------+------------+--------------------+----------+----------+
    | GameID | TeamID | Team               | OpponentID | Opponent           | Location | score    |
    +--------+--------+--------------------+------------+--------------------+----------+----------+
    | 1      | 1      | Alabama            | 2          | Jacksonville State | H        | 1.098633 |
    +--------+--------+--------------------+------------+--------------------+----------+----------+
    | 1      | 2      | Jacksonville State | 1          | Alabama            | V        | 0.756562 |
    +--------+--------+--------------------+------------+--------------------+----------+----------+
    | 2      | 3      | UAB                | 4          | Alcorn State       | H        | 1.270638 |
    +--------+--------+--------------------+------------+--------------------+----------+----------+
    | 2      | 4      | Alcorn State       | 3          | UAB                | V        | 0.682791 |
    +--------+--------+--------------------+------------+--------------------+----------+----------+

Each row represnts one of two teams results from a distinct GameID. My goal is to have a final df that looks like this

+--------+--------+--------------------+------------+--------------------+----------+----------+-----------------+
| GameID | TeamID | Team               | OpponentID | Opponent           | Location | score    | opponents score |
+--------+--------+--------------------+------------+--------------------+----------+----------+-----------------+
| 1      | 1      | Alabama            | 2          | Jacksonville State | H        | 1.098633 | 0.756562        |
+--------+--------+--------------------+------------+--------------------+----------+----------+-----------------+
| 1      | 2      | Jacksonville State | 1          | Alabama            | V        | 0.756562 | 1.098633        |
+--------+--------+--------------------+------------+--------------------+----------+----------+-----------------+
| 2      | 3      | UAB                | 4          | Alcorn State       | H        | 1.270638 | 0.682791        |
+--------+--------+--------------------+------------+--------------------+----------+----------+-----------------+
| 2      | 4      | Alcorn State       | 3          | UAB                | V        | 0.682791 | 1.270638        |
+--------+--------+--------------------+------------+--------------------+----------+----------+-----------------+

I am stuck on how to look up values that match criteria with different column names. Thanks!

1
  • df.merge(df, left_on=['GameID','TeamID'], right_on=['GameID', 'OpponentID'], how'left') and rename accordingly. Commented Apr 6, 2021 at 2:39

2 Answers 2

1

You can make use of merge() method:

resultdf=df.merge(df[['GameID','OpponentID','score']], left_on=['GameID','TeamID'], right_on=['GameID','OpponentID'], how='left')

Now make use of drop() method:

result.drop(columns=['OpponentID_y'])

Finally make use of rename() method:

result=result.rename(columns={'OpponentID_x':'OpponentID','score_x':'score','score_y':'opponents score'})

Now if you print result you will get your desired output

Sign up to request clarification or add additional context in comments.

Comments

0

Using Pandas merge solves your query very easily. You can achieve the desired result using the following code:

df_merge = df.merge(df, on=['GameID','TeamID'], how='outer', suffixes=(None, '_y'))
#print("Merged Dataset")
#display(df_merge)
df_merge.drop(['Team_y', 'OpponentID_y', 'Opponent_y', 'Location_y'], axis=1, inplace=True)
df_merge.rename( columns={'Score_y':'Opponent Score'}, inplace=True)
#print("Final Dataset")
#display(df_merge)

Oputput: Dataset before and after merge

Comments

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.