1

I was doing a study case for my assignment on World Aid and how we as programmers should handle the problem. So I was running this code which cuts out the outliers.

percentiles = data_help['exports'].quantile([0.05,0.95]).values
data_help['exports'][data_help['exports'] <= percentiles[0]] = percentiles[0]
data_help['exports'][data_help['exports'] >= percentiles[1]] = percentiles[1]

When I run this code it returns the error as

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

I did not encounter this problem when I was using my friends computer.

Brody_Brody
  • 185
  • 16

1 Answers1

1

Use .loc:

percentiles = data_help['exports'].quantile([0.05,0.95]).values
data_help.loc[data_help['exports'] <= percentiles[0], 'exports'] = percentiles[0]
data_help.loc[data_help['exports'] >= percentiles[1], 'exports'] = percentiles[1]

Read: How to deal with SettingWithCopyWarning in Pandas

Corralien
  • 109,409
  • 8
  • 28
  • 52