0

So I am trying to create a new column in my pandas dataframe based on some conditionals with respect to some other already established columns. The logic in this code is fairly simple.

conditions_home = [premier_league_75['home_goals']>premier_league_75['away_goals'],
              premier_league_75['home_goals']<premier_league_75['away_goals'],
              premier_league_75['home_goals']==premier_league_75['away_goals']]
values_home = [3,0,1]

premier_league_75['home_points'] = np.select(conditions_home,values_home)

Unfortunately this produces the warning:

SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

The resulting new column produces correct results but I am confused why the code is producing a warning. How would I be able to create this new column using these conditionals and not produce this warning?

  • SettingWithCopyWarning is a wide known python warning... Please refer to https://realpython.com/pandas-settingwithcopywarning/. – Renato Aranha Apr 01 '21 at 03:00
  • Yeah I read through that and kinda of understand the warning in general but am having trouble coding around it in this particular situation. If my code is producing the correct output, is it okay to remove the warning? – Ian Dragulet Apr 01 '21 at 03:05
  • 1
    `premier_league_75` might have been derived from another dataframe? if thats the case put a `.copy()` to the end of code where you define `premier_league_75` – anky Apr 01 '21 at 03:13
  • Oh Yes! Thank you, that was it! – Ian Dragulet Apr 01 '21 at 03:23

0 Answers0