Convert Object to Float in Pandas

Convert Object to Float in Pandas

1. Introduction

Pandas is python library for data analysis and manipulation. One of the common tasks is to convert data type of column from object to float. We can achieve this using astype() or to_numeric() methods.

Method 1: Using astype()

Method 2: Using to_numeric()

Let’s see each method in detail with examples.

2. Using astype() Method

Use the astype() method to convert one DataFrame column from object to float in pandas.

Note that marks column has data type of float64.

Use the astype() method to convert multiple columns of DataFrame from object to float in pandas.

Use the astype() method to convert the entire DataFrame from object to float in pandas.

We used the astype() method to convert one column, multiple columns and the entire DataFrame’s dtypes from object to float. This method took a float dtype as a parameter to convert to float. Using astype() depends on the use case, whether you are using it to convert one column, multiple columns or an entire DataFrame; you can refer to the above examples for all these scenarios.

Now, think of a situation where we are supposed to convert the whole DataFrame from object to float while one or multiple columns of this DataFrame are not convertible. Will the astype() method still work? Let’s see the following example.

In the above example, we used the astype() method to convert the entire DataFrame from object to float where all columns are not convertible. So, we will get a ValueError as demonstrated above. Now, how can we handle it?

Yes, we can use the errors attribute set to ignore to leave the entire DataFrame as it is if any of its columns is not convertible from object to float. See the following example.

But, if you still want to convert the convertible columns and leave those that aren’t, we use the replace() method with astype() as follows.

We successfully converted the roll_number and marks columns from object to float but left the students column as it is because the strings are not convertible to float.

We can also use the replace() method with astype() to convert one or multiple columns of a DataFrame from object to float.

3. Using to_numeric() Method

Use the to_numeric() method to convert one DataFrame column from object to float in pandas.

Use the to_numeric() method to convert multiple DataFrame columns from object to float in pandas.

Use the to_numeric() method to convert the entire pandas DataFrame from object to float where all columns are convertible.

The above examples are similar to the code fences learned in the previous section and have the same flow, but we used the pd.to_numeric method this time.

This method converts the object to a numeric data type; it can be a float or integer, depending on the specified value. For example, pd.to_numeric will convert '5' to 5 while '5.0' to 5.0. So, we used the apply() method to apply the pd.to_numeric function to convert multiple columns or complete DataFrame from object to float.

Converting the entire DataFrame, where all columns can not be converted from object to float, will result in the ValueError; see the following example.

We can use the errors = 'ignore' in the apply() method, as shown below, to ignore columns that are not convertible. For example, see the following code snippet.

See, this time, the students column is not converted because it is not convertible, and we have ignored it using the errors attribute in the apply() method.

We can also use the apply() method with the lambda function as df['df_column'] = df['df_column'].apply(lambda x: float(x)) to convert one DataFrame column from object to float, but this approach will not work to convert multiple DataFrame or entire DataFrame; for that, we can use pd.to_numeric(), which we have learned already.

4. Replace invalid values with Nan while conversion

In case, we have any invalid values in column and cannot be converted to float, we can use errors parameter with value as coerce, it will convert invalid value to NaN.

As you can see, AB value in marks column has been converted to NaN.

5. Conclusion

In this article, we explored how to convert object to float in Pandas using astype() and to_numeric() methods.

We can use errors parameter with value as ignore with both methods if some of columns are not convertible to float and we are still trying to convert complete dataframe or non convertible datatype columns to float.

Was this post helpful?

Leave a Reply

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