1

This question is somewhat similar to: Remap values in pandas column with a dict, however, the answers are quite dated and do not cover the "SettingWithCopyWarning".

I am simply trying to replace the original strings in a column, "col", of my dataframe, "df", using a dictionary, "dict1". Here is my code which successfully replaces the values:

    temp_series = df.loc[:,col].copy()
    for name in temp_series:
        for old, new in q_names_dict.items():
            if (old.lower() == name.lower()):
                temp_series.replace(name, new, inplace=True)

-However, when I attempt to update my original dataframe with this copy, "temp_series", I get a "SettingWithCopyWarning". Here is the code which throws that warning:

df.loc[:,col] = temp_series
# The bottom three don't work either.
#df[col] = temp_series 
#df.loc[:,col].update(temp_series) 
#df[col].update(temp_series)      
django_moose
  • 355
  • 1
  • 4
  • 10
  • 3
    Any reason you aren't using the `df.replace` method itself (shown in the first answer on the linked post) and avoid the error and explicit looping altogether? – Jon Clements Aug 13 '18 at 16:47
  • The df.replace method is quicker and better, however it does not allow for any regex to ignore the case between the dict's values and the original strings. That is why I use the loops and call the '.lower()' method on strings. – django_moose Aug 13 '18 at 17:52
  • Can't you prefix it with (?i) to make it case insensitive? – Jon Clements Aug 13 '18 at 18:08
  • My apologies, the dataframe version of .replace() does allow for regex. However, I don't really know how I can prefix the regex for ignorecase ( (?i) ), with the syntax in the replace function. Example, in the link I provided above, the user says to do: "df.replace({"col1": di})". How can you add regex to that? Thank you – django_moose Aug 13 '18 at 18:16
  • 1
    Was just an idea I was throwing out there - on mobile at the moment so can't check/look properly. – Jon Clements Aug 13 '18 at 18:18

1 Answers1

0

By changing the deep copy to a shallow copy, I was able to have changes to the original dataframe, "df". It is stated in the docs: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.copy.html

Here is the code that enables shallow copy:

temp_series = df.loc[:,col].copy(deep=False)

Therefore, I don't have to explicitly update my dataframe after using the above code. This also prevents the SettingWithCopyWarning.

django_moose
  • 355
  • 1
  • 4
  • 10