Reputation: 49
I have the following columns in my dataframe:
I added a new column to my dataframe so that it displays the name of the winner:
If Team 1 goals > Team 2 goals, then the winner is Team 1 name.
I used the following function and it works as expected:
def winner(row):
if row['Team 1 goals'] > row['Team 2 goals']:
return row['Team 1 name']
elif row['Team 2 goals'] > row['Team 1 goals']:
return row['Team 2 name']
else:
return 'Draw'
df['Winner of The Game'] = df.apply(winner, axis=1)
But then, I need to use this block of code as a class.
I use a class like this:
class winner:
def __init__(self,row):
self.row=row
def winner(row):
if self.row['Team 1 goals'] > self.row['Team 2 goals']:
return obj['Team 1 name']
elif self.row['Team 2 goals'] > self.row['Team 1 goals']:
return self.row['Team 2 name']
else:
return 'Draw'
At that point, the final output in my table will be like this (it doesn't display the correct name of the winner team):
How should I fix my class so that it shows the correct winner team's name?
Upvotes: 0
Views: 128
Reputation: 6748
Here is simpler version of your code block
class winner:
def __init__(self,row):
self.row=row
def winner(row):
return 'Draw'
df = pd.DataFrame([[1],[2],[3]],columns=['a'])
df['new']= df.apply(winner.winner,axis=1)
df
Upvotes: 2