Convert String to Float in pandas DataFrame Column in Python (4 Examples)

 

This tutorial shows how to convert a string column to the float data type in a pandas DataFrame in Python programming.

The tutorial will consist of this information:

Let’s jump right to the examples.

 

Example Data & Software Libraries

We first need to load the pandas library to Python:

import pandas as pd                                     # Load pandas

Furthermore, have a look at the following example data:

data = pd.DataFrame({'x1':['1.1', '7.2', '5.3', '4.4', '7.5', '5.6'], # Create DataFrame
                     'x2':['10.1', '2.1', '9.1', '15.1', '22.1', '5.1'],
                     'x3':['3', '4', '5', '8', '1', '6']})
print(data)                                             # Print pandas DataFrame

 

table 1 DataFrame convert string float pandas dataframe column python

 

Table 1 shows the structure of our exemplifying pandas DataFrame: It is made of six rows and three columns.

Let’s also print the data types of the columns in our pandas DataFrame:

print(data.dtypes)                                      # Check data types of columns
# x1    object
# x2    object
# x3    object
# dtype: object

At this point, all columns in our data set have the object data type. Note that the pandas library stores strings as object dtypes.

Let’s convert these objects (or strings) to the float data type!

 

Example 1: Convert Single pandas DataFrame Column from String to Float

This example shows how to convert only one specific variable in a pandas DataFrame to the float data class.

For this task, we can use the astype function as shown in the following Python code:

data_new1 = data.copy()                                 # Create copy of DataFrame
data_new1['x1'] = data_new1['x1'].astype(float)         # Transform string to float

After executing the previous syntax, a new pandas DataFrame called data_new1 has been created. Let’s print the classes of all columns of this new data set:

print(data_new1.dtypes)                                 # Check data types of columns
# x1    float64
# x2     object
# x3     object
# dtype: object

As you can see, we have changed the data type of the column x1 from object (or string) to the float64 class.

 

Example 2: Convert Multiple pandas DataFrame Columns from String to Float

It is also possible to transform multiple pandas DataFrame columns to the float data type.

To accomplish this, we can apply the Python code below:

data_new2 = data.copy()                                 # Create copy of DataFrame
data_new2 = data_new2.astype({'x2': float, 'x3': float}) # Transform multiple strings to float

Let’s check the classes of our columns once again:

print(data_new2.dtypes)                                 # Check data types of columns
# x1     object
# x2    float64
# x3    float64
# dtype: object

This time, we have set the variables x2 and x3 to the float data type. The column x1 is still a string.

 

Example 3: Convert All pandas DataFrame Columns from String to Float

The following syntax shows how to switch the data type of all pandas DataFrame columns from string to float.

Once again, we can apply the astype function for this:

data_new3 = data.copy()                                 # Create copy of DataFrame
data_new3 = data_new3.astype(float)                     # Transform all columns to float

Let’s have another look at the classes of our columns:

print(data_new3.dtypes)                                 # Check data types of columns
# x1    float64
# x2    float64
# x3    float64
# dtype: object

All column classes have been changed from string to float.

 

Example 4: Convert pandas DataFrame Column from String to Float Using to_numeric() Function

In the previous examples, we have used the astype function to adjust the data type of certain pandas DataFrame columns from string to float.

In this example, I’ll illustrate how to use the to_numeric function instead.

Consider the Python code below:

data_new4 = data.copy()                                 # Create copy of DataFrame
data_new4['x1'] = pd.to_numeric(data_new4['x1'])        # Apply to_numeric function

Now, let’s print the data types of the columns of our new data set:

print(data_new4.dtypes)                                 # Check data types of columns
# x1    float64
# x2     object
# x3     object
# dtype: object

Similar to Example 1, we have transformed the first column of our pandas DataFrame to the float data type.

 

Video & Further Resources

Would you like to learn more about the conversion of a string column to a float in a pandas DataFrame? Then I recommend watching the following video on my YouTube channel. I’m explaining the Python programming codes of this article in the video.

 

The YouTube video will be added soon.

 

Furthermore, you could read some of the other tutorials on https://statisticsglobe.com/.

 

At this point you should know how to transform and parse a string column to the float data type in the Python programming language. Don’t hesitate to let me know in the comments section, if you have additional questions or comments. Besides that, please subscribe to my email newsletter in order to receive updates on the newest articles.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top