Tiboo
Tiboo

Reputation: 27

Issue with python loc on dataframe

I'm currently working on a project on python with jupyter notebook. I would like to predict stadium attendance in france (ligue 1).

To achieve that I have taken data from web with beautiful soup. I'm trying now to clean up my data : I have some missing values for the stadiums and I would like to assign stadium for a specific team (Olympique lyonnais).

I first tried that :

stats_match.stade[(stats_match.saison >= 2017) & (stats_match.domicile == 'Olympique Lyonnais') & (stats_match.stade.isna())] = 'Groupama stadium'

which gave me that error :

SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame

So I followed the instruction and tried that :

stats_match.stade.loc((stats_match.saison >= 2017) & (stats_match.domicile == 'Olympique Lyonnais') & (stats_match.stade.isna())) = 'Groupama stadium' 

which give me :

File "", line 3 stats_match.stade = stats_match.stade.loc((stats_match.saison >= 2017) & (stats_match.domicile == 'Olympique Lyonnais') & (stats_match.domicile.isna())) = 'Groupama stadium'
SyntaxError: can't assign to function call

What do I miss here ? Do I have to use .where function ? Many thanks

Upvotes: 0

Views: 161

Answers (2)

Rajat Jain
Rajat Jain

Reputation: 2032

This should be correct;

stats_match.loc[(stats_match.saison >= 2017) & (stats_match.domicile == 'Olympique Lyonnais') & (stats_match.stade.isna())] = 'Groupama stadium'

Upvotes: 1

Tiboo
Tiboo

Reputation: 27

Okay like @roganjosh said I put () instead of [], but I have now the same warning "SettingWithCopyWarning, I already had this warning and used .copy(), but is there a better way of doing it ?

Upvotes: 0

Related Questions