0

I am trying to convert a column to float from str type. I am using .loc because there is a SettingWithCopyWarning error. But after using .loc, the warning still did not disappear. Can you guys please help me to see where the problem is?

The line I am using to convert column "A" is as follows:

df.loc[:, "A"] = pd.to_numeric(df.loc[:, "A"].copy())

Column "A" has bunch of numbers who are currently in str format.

  • At some point _before_ this provided code you have unsafely subset your DataFrame. Either `new_df = df[cols]` or `new_df = df[mask]` when it should have been `new_df = df[cols].copy()` or `new_df = df[mask].copy()` The warning is letting you know that `df[mask]['col'] = value` may not work because `df[mask]` may produce a copy and recommends that you use `df.loc[mask, 'col'] = value` but that message is not clear here since you're doing something like `new_df = df[mask]` then later doing `new_df[col] = value` which looks to pandas like a (deferred) `df[mask][col] = value` call. – Henry Ecker Feb 26 '22 at 17:25

0 Answers0