0

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

5
  • The first issue is that .loc uses square brackets, not .loc() Commented Mar 1, 2019 at 15:27
  • Second, I don't think you want to be calling this on the series, but rather on the df itself Commented Mar 1, 2019 at 15:29
  • I do, because If I call it on the dataframe It would assign my value on all the columns, no ? Commented Mar 1, 2019 at 15:37
  • No, because loc allows you to specify the column that you want to transform, separated from your conditions by a comma but still inside the square brackets Commented Mar 1, 2019 at 15:38
  • It works thanks ! didn't know I could specify the colunm in the loc function. Commented Mar 1, 2019 at 15:41

2 Answers 2

1

This should be correct;

stats_match.loc[(stats_match.saison >= 2017) & (stats_match.domicile == 'Olympique Lyonnais') & (stats_match.stade.isna())] = 'Groupama stadium'
Sign up to request clarification or add additional context in comments.

Comments

0

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 ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.