Pandas convert column to float

In this post, we will see how to convert column to float in Pandas.

Convert String column to float in Pandas

There are two ways to convert String column to float in Pandas.

Using asType(float) method

You can use asType(float) to convert string to float in Pandas.
Here is the syntax:

Here is an example. We will convert data type of Column Rating from object to float64
Sample Employee data for this Example.

NameRating
Andy8.6
Prateek7

Output:

Using toNumeric() method

You can use toNumeric() method where you might have non-numeric values in the column.
Here is the syntax:

If column as non-numeric values, then they will be converted to NaN.
Here is sample Employee Data for this example.

NameRating
Andy8.6
RiyankaNA
Prateek7
NehaNA
Let’s say Rating is not available for new employee Riyanka and Sneha. In this scenario, we will convert Rating column data type from String to float using toNumeric() method.

Output:

As you can see, we have converted Rating column to float64.

Convert Integer column to float in Pandas

There are two ways to convert Integer column to float in Pandas.

Using asType(float) method

You can use asType(float) to convert string to float in Pandas.
Here is the syntax:

Here is an example. We will convert data type of Column Salary from integer to float64

Output:

Using toNumeric() method

You can use toNumeric() method where you might have non-numeric values in the column.
Here is the syntax:

Here is an example:

Output:

That’s all about pandas convert column to float.

Was this post helpful?

Leave a Reply

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