-1

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?

tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • 2
    We can't create dataframes from images. Please post code that creates the sample dataframe instead of an image. – tdelaney Dec 30 '20 at 17:17
  • pandas `series.str.replace()` only works on series objects, not whole dataframes. Iterating through your columns and doing the replacement on each in turn is one way to solve it – G. Anderson Dec 30 '20 at 17:19
  • What is `new_df.dtypes`? The columns without commas are likely floats. – tdelaney Dec 30 '20 at 17:19
  • This is a screenshot of the text file I am using as my dataframe. should I copy-paste the txt file here? – Ucchash Sarkar Dec 30 '20 at 17:20
  • 2
    @UcchashSarkar yes, you should usually do so. – Quang Hoang Dec 30 '20 at 17:20
  • 1
    [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – wwii Dec 30 '20 at 17:20

2 Answers2

2

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)
         )
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • Its not `regex=True` that fixes the problem (its using `DataFrame.replace`) and since regex is slower than string replace it should only be used when needed. Otherwise this is a greate answer. – tdelaney Dec 30 '20 at 17:34
0

You can apply the str method only in pandas series.

You can try this for several columns:

new_df = new_df[['Eleven', 'Twelve', 'Thirteen', 'Fourteen']].apply(lambda x : x.replace(",","").astype(float)
ljuk
  • 701
  • 3
  • 12