Convert Integer to Float in pandas DataFrame Column in Python (5 Examples)
This article explains how to convert an integer column in in a pandas DataFrame to the float data type in the Python programming language.
Table of contents:
Let’s jump right to the examples.
Example Data & Add-On Libraries
First, we have to import the pandas library to Python:
import pandas as pd # Import pandas library to Python
Let’s also create some example DataFrame using the pandas library in Python:
data = pd.DataFrame({'x1':range(2, 7), # Create pandas DataFrame 'x2':range(7, 2, - 1), 'x3':range(12, 17)}) print(data) # Print pandas DataFrame # x1 x2 x3 # 0 2 7 12 # 1 3 6 13 # 2 4 5 14 # 3 5 4 15 # 4 6 3 16
The previous output of the Python console shows the structure of our example data – It contains five rows and three columns.
Let’s check the data types of the columns in our data set:
print(data.dtypes) # Check data types of columns # x1 int64 # x2 int64 # x3 int64 # dtype: object
As you can see, all of our three columns have the integer class.
Example 1: Convert Single pandas DataFrame Column from Integer to Float
This example explains how to convert one single column from the integer data type to float.
To accomplish this task, we can apply the astype function as you can see in the following Python code:
data_new1 = data.copy() # Create copy of DataFrame data_new1['x1'] = data_new1['x1'].astype(float) # Transform integer to float print(data_new1) # Print updated pandas DataFrame # x1 x2 x3 # 0 2.0 7 12 # 1 3.0 6 13 # 2 4.0 5 14 # 3 5.0 4 15 # 4 6.0 3 16
Have a look at the previous output. As you can see, we have created a new data set called data_new1, which contains the same three column names as our input data set. However, you can already see that the column x1 is shown with decimal points.
The reason for that gets obvious when we check the classes of our DataFrame columns once again:
print(data_new1.dtypes) # Check data types of columns # dtype: object # x1 float64 # x2 int64 # x3 int64 # dtype: object
As you can see, we have converted the first column in our new pandas DataFrame from integer to the float data type.
Example 2: Convert Multiple pandas DataFrame Columns from Integer to Float
It is also possible to transform multiple variables to a different data type.
In Example 2, I’ll show how to change the data class of two variables from integer to float.
Once again, we can use the astype function for this:
data_new2 = data.copy() # Create copy of DataFrame data_new2 = data_new2.astype({'x2': float, 'x3': float}) # Transform multiple floats to string print(data_new2) # Print updated pandas DataFrame # x1 x2 x3 # 0 2 7.0 12.0 # 1 3 6.0 13.0 # 2 4 5.0 14.0 # 3 5 4.0 15.0 # 4 6 3.0 16.0
Let’s have another look at the data types of our pandas DataFrame columns:
print(data_new2.dtypes) # Check data types of columns # x1 int64 # x2 float64 # x3 float64 # dtype: object
This time, we have changed the data types of the columns x2 and x3 to the float class.
Example 3: Convert All pandas DataFrame Columns from Integer to Float
The Python syntax below illustrates how to modify all column data types at once.
For this, we can apply the astype function as shown in the following Python code:
data_new3 = data.copy() # Create copy of DataFrame data_new3 = data_new3.astype(float) # Transform all columns to string print(data_new3) # Print updated pandas DataFrame # x1 x2 x3 # 0 2.0 7.0 12.0 # 1 3.0 6.0 13.0 # 2 4.0 5.0 14.0 # 3 5.0 4.0 15.0 # 4 6.0 3.0 16.0
Again, let’s test what classes our updated DataFrame columns have:
print(data_new3.dtypes) # Check data types of columns # x1 float64 # x2 float64 # x3 float64 # dtype: object
As expected – All columns have the float class!
Example 4: Convert pandas DataFrame Column from Integer to Float Using apply() Function
In the previous examples, I have explained how to use the astype function to adjust the data types of pandas DataFrame columns.
In Examples 4 and 5, I want to show you how to use different functions for this task.
The following Python code demonstrates how to use the apply function to convert an integer column to the float class:
data_new4 = data.copy() # Create copy of DataFrame data_new4['x1'] = data_new4['x1'].apply(float) # Transform string to float
Have a look at the updated data types of our new data set:
print(data_new4.dtypes) # Check data types of columns # x1 float64 # x2 int64 # x3 int64 # dtype: object
Similar to Example 1, we have transformed the first column of our input DataFrame from the integer class to the float data type.
Example 5: Convert pandas DataFrame Column from Integer to Float Using to_numeric() Function
Example 5 shows how to use the to_numeric to convert a single column from integer to float.
For this, we have to specify the downcast argument within the to_numeric command to be equal to ‘float’:
data_new5 = data.copy() # Create copy of DataFrame data_new5['x1'] = pd.to_numeric(data_new5['x1'], downcast = 'float') # Apply to_numeric function
Again, we have modified the data type of the column x1:
print(data_new5.dtypes) # Check data types of columns # x1 float32 # x2 int64 # x3 int64 # dtype: object
Video, Further Resources & Summary
Some time ago, I have published a video on my YouTube channel, which illustrates the topics of this tutorial. You can find the video below:
The YouTube video will be added soon.
Furthermore, you might read the other articles on my website:
- Handling DataFrames Using the pandas Library in Python
- Convert Float to String in pandas DataFrame Column in Python
- Convert String to Float in pandas DataFrame Column in Python
- Get Max & Min Value of Column & Index in pandas DataFrame in Python
- Check if Column Exists in pandas DataFrame in Python
- Convert pandas DataFrame to NumPy Array in Python in R
- Convert Index to Column of pandas DataFrame in Python
- Introduction to Python
To summarize: You have learned in this article how to transform an integer column in in a pandas DataFrame to a float in the Python programming language. If you have additional questions and/or comments, please let me know in the comments.