Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Comparing pandas columns and map

I have a pandas dataframe as follows.

    home_team_goal  away_team_goal
id      
1   1               1
2   0               0
3   0               3
4   5               0
5   1               3

I need to introduce a new column called match_status using the logic below.

def match_status(home_goals, other_goals):
    if home_goals > other_goals:
        return 'WIN'
    elif home_goals < other_goals:
        return 'LOSE'
    else:
        return 'DRAW'

df_match.apply(match_status, 1, df_match['home_team_goal'], df_match['away_team_goal'])

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But it give the following errors.

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-83-2e2115165ea9> in <module>()
----> 1 df_match.apply(match_status, 1, df_match['home_team_goal'], df_match['away_team_goal'])

3 frames

/usr/local/lib/python3.7/dist-packages/pandas/core/generic.py in __nonzero__(self)
   1536     def __nonzero__(self):
   1537         raise ValueError(
-> 1538             f"The truth value of a {type(self).__name__} is ambiguous. "
   1539             "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
   1540         )

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

What is the correct way to apply the above and get a new column?

>Solution :

Problem is that if home_goals > other_goal returns Series, you can try np.select

df['match_status'] = np.select(
    [df_match['home_team_goal'] > df_match['away_team_goal'],
     df_match['home_team_goal'] < df_match['away_team_goal'],],
    ['WIN', 'LOSE'],
    'DRAW'
)

Or modify your apply

df_match.apply(lambda row: match_status(row['home_team_goal'], row['away_team_goal']), 1, )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading