I have this 'Club' column in my pandas df that contains the names of English Premier League clubs but the naming of the club is not suited for what I want to achieve. I tried writing a function with conditional statements to populate another column with the names of the club in the format I want. I have tried applying my function to my df but I get this error:
ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index 0')
This is what df called test looks like: test df
This is my function called clubs_name:
#We want to rename the clubs to be exact names like in Squad column in the epl_table_df dataframe
def clubs_name(Club):
if Club == 'Leicester City LEI':
return 'Leicester City'
elif Club == 'Tottenham Hotspur TOT':
return 'Tottenham'
elif Club == 'Liverpool LIV':
return 'Liverpool'
elif Club == 'Southampton SOU':
return 'Southampton'
elif Club == 'Chelsea CHE':
return 'Chelsea'
elif Club == 'Aston Villa AVL':
return 'Aston Villa'
elif Club == 'Everton EVE':
return 'Everton'
elif Club == 'Crystal Palace CRY':
return 'Crystal Palace'
elif Club == 'Wolverhampton Wanderers WOL':
return 'Wolves'
elif Club == 'Manchester City MCI':
return 'Manchester City'
elif Club == 'Arsenal ARS':
return 'Arsenal'
elif Club == 'West Ham United WHU':
return 'West Ham'
elif Club == 'Newcastle United NEW ':
return 'Newcastle Utd'
elif Club == 'Manchester United MUN':
return 'Manchester Utd'
elif Club == 'Leeds United LEE':
return 'Leeds United'
elif Club == 'Brighton and Hove Albion BHA':
return 'Brighton'
elif Club == 'Fulham FUL':
return 'Fulham'
elif Club == 'West Bromwich Albion WBA':
return 'West Brom'
elif Club == 'Burnley BUR':
return 'Burnley'
elif Club == 'Sheffield United SHU':
return 'Sheffield Utd'
else:
return Club'
When I test my function, it seems to be working:
print(clubs_name('Fulham FUL'))
This was how I tried to apply the function to the test df:
test.apply (lambda Club: clubs_name(Club), axis=1)
I am new to python and data science/analysis. I will appreciate a solution, an explanation of the error and what I was doing wrong.