Home > OS >  How to Remove quotation mark with object data type from a column in Python and convert to float
How to Remove quotation mark with object data type from a column in Python and convert to float

Time:11-22

Customer id ----- object

ValueError: could not convert string to float: "'5769842393258'"

df["Customer id"] = df["Customer id"] .replace('"', '', regex=True)

df["Customer id"] = np.array(df["Customer id"],dtype=float)

CodePudding user response:

Try...

df["Customer id"] = float(df["Customer id"])

CodePudding user response:

You might simply use .str.strip method as follows

import pandas as pd
df = pd.DataFrame({'X':["'123'","'456'","'789'"]})
df['Xnum'] = df['X'].str.strip("'").astype(float)
print(df)

output

       X   Xnum
0  '123'  123.0
1  '456'  456.0
2  '789'  789.0

Explanation: .str allow using String Methods on strings which are inside pandas.Series (column of pandas.DataFrame)

  • Related