Pandas convert column to float

Previous
Next

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.

Name Rating
Andy 8.6
Prateek 7

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.

Name Rating
Andy 8.6
Riyanka NA
Prateek 7
Neha NA

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.

Previous
Next

Add Comment