-1

I got an error in my code while converting string to character.

This is my dataframe image:

Photo

This how I remove the unit in the data:

Photo

and then while converting with:

for i in ['Power']:
    data[i] = data[i].astype(float)

I got the following error message:

Error message

Joundill
  • 6,828
  • 12
  • 36
  • 50
MRSL
  • 23
  • 1
  • 5

1 Answers1

0
import pandas as pd
data = {'Power': ['58.6 bhp', '126.2 bhp', '88.7 bhp']}
df = pd.DataFrame(data)


df=df.replace({'Power':'[a-z]'},'',regex=True) 

you can use lambda for row wise operation and convert to "float" as below :

df['Power']=df['Power'].apply(lambda row: float(row))

notebook Image

vbhatt
  • 48
  • 4