Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Converting str to float in pandas warning

I am trying to convert str to float in a pandas data frame and get the following error:

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

The code:

df_clean['a'] = df_clean['a'].astype(float)

Should I ignore it? Can it be written without generating the warning?
Thanks

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Maybe try using to_numeric

import pandas as pd
df_clean['a'] = pd.to_numeric(df_clean['a'], errors='ignore')

The "errors" flag gives you different options for how to handle ones that cannot be converted.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading