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

  • 05 October

    Pandas replace values in column

    Table of ContentsUsing the loc() function to replace values in column of pandas DataFrameUsing the iloc() function to to replace values in column of pandas DataFrameUsing the map() function to replace values of a column in a pandas DataFrameUsing the replace() function to replace values in column of pandas DataFrameUsing the where() function to replace […]

  • Select rows by multiple conditions using loc in Pandas
    29 July

    Select rows by multiple conditions using loc in Pandas

    The loc() function in a pandas module is used to access values from a DataFrame based on some labels. It returns the rows and columns which match the labels. We can use this function to extract rows from a DataFrame based on some conditions also. First, let us understand what happens when we provide a […]

  • Split dataframe in Pandas
    28 July

    Split dataframe in Pandas

    Table of ContentsUsing the iloc() function to split DataFrame in PythonBy RowsBy ColumnsUsing the sample() function to split DataFrame in PythonUsing the groupby() function to split DataFrame in PythonUsing the columns to split DataFrame in Python In real-life scenarios, we deal with massive datasets with many rows and columns. At times, we may want to […]

  • Read text file in Pandas
    28 July

    Read text file in Pandas

    Table of ContentsUsing the read_csv() function to read text files in PandasUsing the read_table() function to read text files in PandasUsing the read_fwf() function to read text files in Pandas A dataset has the data neatly arranged in rows and columns. The pandas module in Python allows us to load DataFrames from external files and […]

  • 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 […]

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.