Home > Blockchain >  Comparing pandas columns and map
Comparing pandas columns and map

Time:08-14

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'])

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?

CodePudding user response:

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, )
  • Related