pythonpandaslambdawarningspandas-settingwithcopy-warning

avoid SettingWithCopyWarning when using a function


I have DataFrame and I need to derivate a new column based on the value of an existing column.

class SomeClass:
  def reduce(self, x):
    if x < 1:
        return x ** 2
    return np.sqrt(x)    


  def penalize_performance(self, df):
    df['sqrt'] = df.apply(lambda x : self.reduce(x.perf), axis=1)
    return df

The result is correct but I get the SettingWithCopyWarning warning:

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

How to I fix the syntax, to avoid the issue ?


Solution

  • The best way i found to avoid SettingWithCopyWarning, (which basically warns you to check your results, as the method chaining, might not work as intended, meaning, it could update only a subset of your DataFrame" copy of the original DatFrame" instead of your original DataFrame)

    is to use .loc[]

    class SomeClass:
      def reduce(self, x):
        if x < 1:
            return x ** 2
        return np.sqrt(x)    
    
    
      def penalize_performance(self, df):
        df.loc[:, 'sqrt'] = df.apply(lambda x : self.reduce(x.perf), axis=1) #Edited line using .loc[] to update a dataframe.
        return df