0

I am new here. Here's my problem: I am trying to drop the rows in my data frame given a range of value. Code:

outlier_age = df3[ (df3['Age']<= 17.) & (df3['Age'] >= 75.) ].index

df4 = df3.drop(outlier_age , inplace=True)

and I am getting this error:

/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py:4169: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy errors=errors,

I feel like it has something to do with datatypes? I tried the == operator just to get to try to drop at least the rows with the given value but it didn't worked.

I tried the same method on a different column, using a string and I was able to drop the rows with the specified string.

Grayrigel
  • 3,474
  • 5
  • 14
  • 32
Queenie
  • 1
  • 3
  • Does this help ? https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas – Grayrigel Oct 21 '20 at 21:41

2 Answers2

0

You can try this.

df4 = df3.drop(df3.index[(df3['Age']<= 17.) & (df3['Age'] >= 75.)])
Dharman
  • 30,962
  • 25
  • 85
  • 135
0

You have an either/or problem here. Either

df3.drop(outlier_age , inplace=True)

which edits the underlying dataframe, or

df4 = df3.drop(outlier_age)

which returns a reduced dataframe. You could also do it in a one-liner:

df4 = df3[ (df3['Age']>= 17.) & (df3['Age'] <= 75.) ]
sjc
  • 1,117
  • 3
  • 19
  • 28