How do I convert floats to integers in Pandas?

How do I convert floats to integers in Pandas?

The process of converting float values to integer values in Pandas involves using the “astype()” method, which allows for data type conversion in a Pandas DataFrame. This method can be applied to a specific column or the entire DataFrame, and it converts all float values to their corresponding integer values. This process is useful when working with data that requires integer values for analysis or when dealing with mixed data types. By using the “astype()” method, float values can be efficiently and accurately converted to integers in Pandas.

Convert Floats to Integers in Pandas


You can use the following syntax to convert a column in a pandas DataFrame from a float to an integer:

df['float_column'] = df['float_column'].astype(int)

The following examples show how to use this syntax in practice.

Example 1: Convert One Column from Float to Integer

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'player': ['A', 'B', 'C', 'D', 'E'],
                   'points': [25.2, 27.0, 14.5, 17.6, 20.7],
                   'assists': [5.1, 7.7, 10.3, 8.6, 9.5]})

#view data types for each column
df.dtypes

player      object
points     float64
assists    float64
dtype: object

We can see that the points and assists columns both have a data type of float.

The following code shows how to convert the points column from a float to an integer:

#convert 'points' column to integer
df['points'] = df['points'].astype(int)

#view data types of each column
df.dtypes

player      object
points       int32
assists    float64
dtype: object

We can see that the points column is now an integer, while all other columns remained unchanged.

Example 2: Convert Multiple Columns to Integer

The following code shows how to convert multiple columns in a DataFrame from a float to an integer:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'player': ['A', 'B', 'C', 'D', 'E'],
                   'points': [25.2, 27.0, 14.5, 17.6, 20.7],
                   'assists': [5.1, 7.7, 10.3, 8.6, 9.5]})

#convert 'points' and 'assists' columns to integer
df[['points', 'assists']] = df[['points', 'assists']].astype(int)

#view data types for each column
df.dtypes

player     object
points      int32
assists     int32
dtype: object

We can see that the points and assists columns have both been converted from floats to integers.

Additional Resources

The following tutorials explain how to perform other common conversions in Python:

Cite this article

stats writer (2024). How do I convert floats to integers in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-i-convert-floats-to-integers-in-pandas/

stats writer. "How do I convert floats to integers in Pandas?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-do-i-convert-floats-to-integers-in-pandas/.

stats writer. "How do I convert floats to integers in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-i-convert-floats-to-integers-in-pandas/.

stats writer (2024) 'How do I convert floats to integers in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-i-convert-floats-to-integers-in-pandas/.

[1] stats writer, "How do I convert floats to integers in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How do I convert floats to integers in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top