0

I am trying to convert the content of a column from Dtype=object to Dtype=float64

My code:

df3["Undernourished"] = df3["Undernourished"].astype(str).astype(float)

The code returns the following warning:

<ipython-input-24-f03715ac98fd>:1: SettingWithCopyWarning: 
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: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  df3["Undernourished"] = df3["Undernourished"].astype(str).astype(float)

The conversion seems to have worked; but am confused about the warning message. Can someone please explain?

unaied
  • 197
  • 11
  • 2
    looks like `df3` is a subset of some other dataframe? if yes, put a `.copy()` to the end of the code where you define `df3` – anky Nov 06 '20 at 10:09
  • 1
    Does this answer your question? [How to deal with SettingWithCopyWarning in Pandas](https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas) – Serial Lazer Nov 06 '20 at 10:11
  • adding .copy() at the end of the code returns the same warning message. Also df3 is not a subset of other dataframes. I copied the content of another df to create df3 – unaied Nov 06 '20 at 10:16
  • "I copied the content of another df to create df3" thats what I meant , add a `.copy()` to that line , example `df3= yourdf.copy()` – anky Nov 06 '20 at 10:20

0 Answers0