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

 

In this Python tutorial you’ll learn how to convert a float column to the integer data type in a pandas DataFrame.

The post will contain these topics:

Let’s dive into it!

 

Example Data & Add-On Libraries

We first have to load the pandas library, if we want to apply the functions that are included in the library:

import pandas as pd                                   # Import pandas library in Python

We also have to construct some data that we can use in the following examples:

data = pd.DataFrame({'x1':[4, 7, 3, 5, 1, 7.5],       # Create pandas DataFrame
                     'x2':[2, 9, 1.1, 1, 9, 7],
                     'x3':[8.2, 1, 9, 8, 1, 7]})
print(data)                                           # Print pandas DataFrame
#     x1   x2   x3
# 0  4.0  2.0  8.2
# 1  7.0  9.0  1.0
# 2  3.0  1.1  9.0
# 3  5.0  1.0  8.0
# 4  1.0  9.0  1.0
# 5  7.5  7.0  7.0

Have a look at the previous Python console output. It shows that our pandas DataFrame contains six rows and three columns.

Let’s check the classes of our DataFrame columns:

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

All columns in our example data set have the float data type.

 

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

In this section, I’ll show how to convert one single column of a pandas DataFrame from the float class to the integer data type in Python.

To accomplish this, we can apply the astype function as you can see below:

data_new1 = data.copy()                               # Create copy of DataFrame
data_new1['x1'] = data_new1['x1'].astype(int)         # Transform float to integer
print(data_new1)                                      # Print updated pandas DataFrame
#    x1   x2   x3
# 0   4  2.0  8.2
# 1   7  9.0  1.0
# 2   3  1.1  9.0
# 3   5  1.0  8.0
# 4   1  9.0  1.0
# 5   7  7.0  7.0

The previous console output already shows a difference – the column x1 does not show decimal points anymore. Furthermore, the value 7.5 in the last row has been rounded to 7.

We can also see this difference when checking the data types of the columns in our new data set:

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

As you can see, the first column x1 has been transformed to the integer class.

 

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

This section demonstrates how to transform multiple variables from the float to the integer data type.

To achieve this, we can use the astype function once again:

data_new2 = data.copy()                               # Create copy of DataFrame
data_new2 = data_new2.astype({'x2': int, 'x3': int})  # Transform multiple floats to integer
print(data_new2)                                      # Print updated pandas DataFrame
#     x1  x2  x3
# 0  4.0   2   8
# 1  7.0   9   1
# 2  3.0   1   9
# 3  5.0   1   8
# 4  1.0   9   1
# 5  7.5   7   7

As you can see, the columns x2 and x3 have been rounded down and do not have any decimal points anymore.

Let’s have another look at the data types of our pandas DataFrame columns:

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

As you can see, the data types of the variables x2 and x3 have been switched from float to integer.

 

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

The following code explains how to change the data types of all columns in a pandas DataFrame from float to integer in the Python programming language.

For this task, we can use the astype function once again. However, this time we are applying this function to the entire data set:

data_new3 = data.copy()                               # Create copy of DataFrame
data_new3 = data_new3.astype(int)                     # Transform all columns to integer
print(data_new3)                                      # Print updated pandas DataFrame
#    x1  x2  x3
# 0   4   2   8
# 1   7   9   1
# 2   3   1   9
# 3   5   1   8
# 4   1   9   1
# 5   7   7   7

As you can see based on the previous output, all columns have been rounded down.

Let’s print the data types of the columns in our new data set:

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

The dtypes of all columns have been changed to the integer class.

 

Example 4: Convert pandas DataFrame Column from Float to Integer Using apply() Function

In Examples 1-3, we have used the astype function. Example 4, in contrast, explains how to use the apply function to convert float columns to the integer data type.

Consider the Python syntax below:

data_new4 = data.copy()                               # Create copy of DataFrame
data_new4['x1'] = data_new4['x1'].apply(int)          # Transform float to integer
print(data_new4)                                      # Print updated pandas DataFrame
#    x1   x2   x3
# 0   4  2.0  8.2
# 1   7  9.0  1.0
# 2   3  1.1  9.0
# 3   5  1.0  8.0
# 4   1  9.0  1.0
# 5   7  7.0  7.0

Now, let’s test the data types of our columns:

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

Similar to Example 1, we have modified only the data type of one specific column.

 

Video, Further Resources & Summary

Do you want to learn more about the transformation of a float column to the integer data type in a pandas DataFrame? Then I can recommend having a look at the following video on my YouTube channel. In the video, I illustrate the examples of this post in Python:

 

The YouTube video will be added soon.

 

Also, you may read some of the related articles on my homepage. I have released several tutorials on related topics such as counting and lists already.

 

Summary: In this tutorial, I have shown how to transform a float column to the integer data type in a pandas DataFrame in the Python programming language. In case you have further questions, don’t hesitate to let me know in the comments below.

 

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