Converting string to float in pandas

enter image description here

I want to convert the strings into float value but there is a comma (“,”) with some values which needed to be removed.

I tried this:

new_df = new_df[['Eleven', 'Twelve', 'Thirteen', 'Fourteen']].str.replace(',', " ").astype(float) 

but it gives me an error:

AttributeError: 'DataFrame' object has no attribute 'str'

Can anyone help me with this?

Answer

Try replace with regex=True option:

new_df = (new_df[['Eleven', 'Twelve', 'Thirteen', 'Fourteen']]
               .replace(',', '', regex=True)
               .astype(float)
         )

Or use apply:

new_df = (new_df[['Eleven', 'Twelve', 'Thirteen', 'Fourteen']]
               .apply(lambda x: x.str.replace(',', ''))
               .astype(float)
         )