Converting str to float in pandas warning

Question:

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

Asked By: Adrian

||

Answers:

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.

Answered By: Cory Randolph
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.