Convert Strings to Floats in Pandas DataFrame

Here are two ways to convert strings to floats in Pandas DataFrame

(1) astype(float)

df["DataFrame Column"] = df["DataFrame Column"].astype(float)

(2) apply(float)

df["DataFrame Column"] = df["DataFrame Column"].apply(float)

Scenarios to Convert Strings to Floats in Pandas DataFrame

Scenario 1: Numeric values stored as strings

To keep things simple, let’s create a DataFrame, where the ‘Price‘ column contains numeric values stored as strings:

import pandas as pd

data = {
"Product": ["aaa", "bbb", "ccc", "ddd", "eee"],
"Price": ["250", "270", "350", "400", "550"],
}

df = pd.DataFrame(data)

print(df)
print(df.dtypes)

Run the code in Python, and you’ll see that the data type for the ‘Price‘ column is Object:

  Product  Price
0     aaa    250
1     bbb    270
2     ccc    350
3     ddd    400
4     eee    550
Product    object
Price      object
dtype: object

The goal is to convert the values under the ‘Price‘ column into floats.

You can then use the astype(float) approach to perform the conversion into floats:

df["DataFrame Column"] = df["DataFrame Column"].astype(float)

For our example, the ‘DataFrame Column’ is the ‘Price‘ column. And so, the full code to convert the values to floats would be:

import pandas as pd

data = {
"Product": ["aaa", "bbb", "ccc", "ddd", "eee"],
"Price": ["250", "270", "350", "400", "550"],
}

df = pd.DataFrame(data)

df["Price"] = df["Price"].astype(float)

print(df)
print(df.dtypes)

You’ll now see that the ‘Price‘ column has been converted into a float:

  Product   Price
0     aaa   250.0
1     bbb   270.0
2     ccc   350.0
3     ddd   400.0
4     eee   550.0
Product     object
Price      float64
dtype: object

You’ll get the same results using apply(float):

import pandas as pd

data = {
"Product": ["aaa", "bbb", "ccc", "ddd", "eee"],
"Price": ["250", "270", "350", "400", "550"],
}

df = pd.DataFrame(data)

df["Price"] = df["Price"].apply(float)

print(df)
print(df.dtypes)

The result:

  Product   Price
0     aaa   250.0
1     bbb   270.0
2     ccc   350.0
3     ddd   400.0
4     eee   550.0
Product     object
Price      float64
dtype: object

Scenario 2: Convert Strings to Floats under an Entire DataFrame

For the second scenario, let’s create a DataFrame with 3 columns, where all the values will be stored as strings:

import pandas as pd

data = {
"Price_1": ["300", "750", "600", "770", "920"],
"Price_2": ["250", "270", "950", "580", "410"],
"Price_3": ["530", "480", "420", "290", "830"],
}

df = pd.DataFrame(data)

print(df)
print(df.dtypes)

As you can see, the data type of all the columns across the DataFrame is object:

  Price_1  Price_2  Price_3
0     300      250      530
1     750      270      480
2     600      950      420
3     770      580      290
4     920      410      830
Price_1    object
Price_2    object
Price_3    object
dtype: object

You can then add the following syntax to convert all the values into floats under the entire DataFrame:

df = df.astype(float)

So the complete Python code to perform the conversion is:

import pandas as pd

data = {
"Price_1": ["300", "750", "600", "770", "920"],
"Price_2": ["250", "270", "950", "580", "410"],
"Price_3": ["530", "480", "420", "290", "830"],
}

df = pd.DataFrame(data)
df = df.astype(float)

print(df)
print(df.dtypes)

All the columns under the entire DataFrame are now floats:

   Price_1  Price_2  Price_3
0    300.0    250.0    530.0
1    750.0    270.0    480.0
2    600.0    950.0    420.0
3    770.0    580.0    290.0
4    920.0    410.0    830.0
Price_1    float64
Price_2    float64
Price_3    float64
dtype: object

You may also want to check the following guides for additional conversions of:

Leave a Comment