0

trying to lower and strip a column in python 3 using panda, but getting the warning-- what is the right way so this warning will not come up

df["col1"] = df[["col1"]].apply(lambda x: x.str.strip())
df["col1"] = df[["col1"]].apply(lambda x: x.str.lower())

The 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

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self[k1] = value[k2]

how to remove the warning

shaswatatripathy
  • 161
  • 1
  • 5
  • 13
  • 2
    I'm guessing `df_rounds_clean` is a subset of another dataframe? – user3483203 Jul 14 '18 at 19:54
  • 2
    Have you read the docs chapter that the warning links to? Is the problem that this doesn't actually apply in your case, so you want to suppress the warning, or that it does apply, so you want to know how to fix it, or that you don't understand it? – abarnert Jul 14 '18 at 20:00
  • 1
    Possible duplicate of [Confusion re: pandas copy of slice of dataframe warning](https://stackoverflow.com/questions/38835483/confusion-re-pandas-copy-of-slice-of-dataframe-warning) – xyzjayne Jul 14 '18 at 21:02
  • 2
    Have you followed the instructions in the warning? – Mad Physicist Jul 15 '18 at 04:35

1 Answers1

1

To get rid of this warning apply it to a series instead of a dataframe. Using df[["col1"]] is creating a new dataframe that you are then setting to the column. If you instead just modify the column it'll be fine. Additionally, I chained the two together.

df["col1"] = df["col1"].str.strip().str.lower()
Neil
  • 14,063
  • 3
  • 30
  • 51