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.

Related Posts

  • Copy DataFrame in Python
    10 July

    Copy DataFrame in Pandas

    This articles provide different ways to copy DataFrame in Pandas.

  • Pandas convert column to int
    18 June

    Pandas convert column to int

    Table of ContentsUse the to_numeric() function to convert column to intUse the astype() function to convert column to intUse the infer_objects() function to convert column to intUse the convert_dtypes() function to convert column to int Pandas is a library set up on top of the Python programming language and is mostly used for the purpose […]

  • 20 September

    Reorder the columns of pandas dataframe in Python

    Table of ContentsUsing reindex methodUsing column selection through column nameUsing column selection through column index In this post, we will see 3 different methods to Reordering the columns of Pandas Dataframe : Using reindex method You can use DataFrame’s reindex() method to reorder columns of pandas DataFrame. You need to pass columns=[$list_of_columns] to reindex() method […]

  • 08 September

    Pandas create Dataframe from Dictionary

    Table of ContentsUsing a Dataframe() method of pandas.Using DataFrame.from_dict() method. In this tutorial, We will see different ways of Creating a pandas Dataframe from Dictionary . Using a Dataframe() method of pandas. Example 1 : When we only pass a dictionary in DataFrame() method then it shows columns according to ascending order of their names […]

  • 08 September

    Pandas Convert list to DataFrame

    In this tutorial, We will see different ways of Creating a pandas Dataframe from List. You can use Dataframe() method of pandas library to convert list to DataFrame. so first we have to import pandas library into the python file using import statement. So let’s see the various examples on creating a Dataframe with the […]

  • 26 May

    Pandas DataFrame to CSV

    Table of ContentsSyntax of DataFrame.to_csv()DataFrame sample dataConvert pandas DataFrame to csv Save CSV file without index column Save CSV file with Custom columns header Save CSV file with selected columns Save CSV file with custom delimiter Save CSV file without header Append data to CSV file Save CSV file with index_label In this post, we […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.