You have a numeric column in pandas which is represented as a string and you want to convert it into a float type.
There are several method for converting a string column to float in pandas, let’s look at them one by one.
Method 1 – astype() –
Let’s read a dataset to illustrate it.
import pandas as pd url = "https://raw.githubusercontent.com/bprasad26/lwd/master/data/QueenCity.csv" df = pd.read_csv(url) df.head()
Let’s also look at the data types of the columns.
Although the Expenditure column contains numeric values but due to addition of dollar ($) signs and commas, it is represented as string (object) in pandas. This is a common problems which people face. Let’s try to convert this to a float type.
First thing we have to do is remove those dollar signs and commas.
# replace dollar sign and commas df['Expenditure'] = df['Expenditure'].str.replace('$', '').str.replace(',', '')
Here, we are doing method chaining to replace dollar signs and commas in one go.
Now, to convert this string column to float we can use the astype method in pandas.
df['Expenditure'] = df['Expenditure'].astype(float) df.dtypes
Related Post –
Method 2 – to_numeric() –
Another method for converting a string column to float is using to_numeric()
df['Expenditure'] = pd.to_numeric(df['Expenditure'], errors='coerce')
error=’coerce’ convert any invalid values into NaN ( Not a number ).