How to Convert Strings to Float in Pandas


Often you may be interested in converting one or more string columns in a pandas DataFrame to float columns. Fortunately this is easy to do using the astype() function.

This tutorial shows several examples of how to use this function in practice using the following DataFrame:

import numpy as np
import pandas as pd

#create DataFrame
df = pd.DataFrame({'points': [np.nan, 12, 15, 14, 19],
                   'assists': ['5', np.nan, '7', '9', '12'],
                   'rebounds': ['11', '8', '10', '6', '6']})  

#view DataFrame
df

 	points	assists	rebounds
0	NaN	5.0	11
1	12.0	NaN	8
2	15.0	7.0	10
3	14.0	9.0	6
4	19.0	12.0	6

#view column data types
df.dtypes

points      float64
assists      object
rebounds     object
dtype: object

Example 1: Convert A Single Column to Float

The following syntax shows how to convert the “assists” column from a string to a float:

#convert "assists" from string to float
df['assists'] = df['assists'].astype(float)

#view column data types
df.dtypes

points      float64
assists     float64
rebounds     object
dtype: object

Example 2: Convert Multiple Columns to Float

The following syntax shows how to convert both the “assists” and “rebounds” columns from strings to floats:

#convert both "assists" and "rebounds" from strings to floats
df[['assists', 'rebounds']] = df[['assists', 'rebounds']].astype(float)

#view column data types
df.dtypes

points      float64
assists     float64
rebounds    float64
dtype: object

Example 3: Convert All Columns to Float

The following syntax shows how to convert all of the columns in the DataFrame to floats:

#convert all columns to float
df = df.astype(float)

#view column data types
df.dtypes

points      float64
assists     float64
rebounds    float64
dtype: object

Example 4: Convert String to Float and Fill in NaN Values

The following syntax shows how to convert the “assists” column from strings to floats and simultaneously fill in the NaN values with zeros:

#convert "assists" from string to float and fill in NaN values with zeros
df['assists'] = df['assists'].astype(float).fillna(0)

#view DataFrame
df

        points	assists	rebounds
0	NaN	5.0	11
1	12.0	0.0	8
2	15.0	7.0	10
3	14.0	9.0	6
4	19.0	12.0	6

Additional Resources

Pandas astype() documentation
Pandas fillna() documentation

Leave a Reply

Your email address will not be published. Required fields are marked *