-1

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 ?

Nicola Lepetit
  • 756
  • 8
  • 25
  • The problem is on the df you pass to your function not your class itself. How do you define this dataframe? for example, is it a sub-part of a bigger dataframe? – Ben.T Oct 05 '22 at 18:20

1 Answers1

1

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
Echo
  • 293
  • 2
  • 10