1

This is my code snippet. The code works however, I am getting the following error:

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

I am guessing this is due to some deprecated syntax...

new_data['mon_fri'] = 0
for i in range(0,len(new_data)):
    if (new_data['Dayofweek'][i] == 0 or new_data['Dayofweek'][i] == 4):
        new_data['mon_fri'][i] = 1
    else:
        new_data['mon_fri'][i] = 0
alex00x0
  • 27
  • 6
  • Possible duplicate of [How to deal with SettingWithCopyWarning in Pandas?](https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas) – anky Feb 23 '19 at 13:19

1 Answers1

2

Dont loop in pandas if exist vectorized alternatives, here is possible use isin for boolean mask and cast to integer for True/False to 1/0 mapping:

new_data['mon_fri'] = new_data['Dayofweek'].isin([0,4]).astype(int)

Or use numpy.where:

new_data['mon_fri'] = np.where(new_data['Dayofweek'].isin([0,4]), 1, 0)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252