Skip to content Skip to sidebar Skip to footer

Creating New Dataframe Data Slicing Issue

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

Solution 1:

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)

Post a Comment for "Creating New Dataframe Data Slicing Issue"