'How to Remove quotation mark with object data type from a column in Python and convert to float
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)
Solution 1:[1]
Try...
df["Customer id"] = float(df["Customer id"])
Solution 2:[2]
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
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Brian |
Solution 2 | Daweo |