Home > Enterprise >  Converting str to float in pandas warning
Converting str to float in pandas warning

Time:08-19

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

CodePudding user response:

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.

  • Related