Table of Contents
Converting all columns of a Pandas DataFrame to integers can be achieved by using the “astype” function. This function allows for the conversion of data types within the DataFrame, including converting columns to integers. By specifying the data type as “int” within the function, all columns in the DataFrame will be converted to integers. This method is useful when dealing with datasets that contain mixed data types, and the need for uniformity in data types is required for further analysis or calculations.
Convert Pandas DataFrame Columns to int
You can use the following syntax to convert a column in a pandas DataFrame to an integer type:
df['col1'] = df['col1'].astype(int)
The following examples show how to use this syntax in practice.
Example 1: Convert One Column 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', '20', '14', '16', '27'], 'assists': ['5', '7', '7', '8', '11']}) #view data types for each column df.dtypes player object points object assists object dtype: object
We can see that none of the columns currently have an integer data type.
The following code shows how to convert the ‘points’ column in the DataFrame to an integer type:
#convert 'points' column to integer
df['points'] = df['points'].astype(int)
#view data types of each column
df.dtypes
player object
points int64
assists object
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 to an integer:
import pandas as pd #create DataFrame df = pd.DataFrame({'player': ['A', 'B', 'C', 'D', 'E'], 'points': ['25', '20', '14', '16', '27'], 'assists': ['5', '7', '7', '8', '11']}) #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 int64 assists int64 dtype: object
We can see that the ‘points’ and ‘assists’ columns have been converted to integer while the ‘player’ column remains unchanged.
The following tutorials explain how to perform other common conversions in Python:
Cite this article
stats writer (2024). How can I convert all columns of a Pandas DataFrame to integers?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-convert-all-columns-of-a-pandas-dataframe-to-integers/
stats writer. "How can I convert all columns of a Pandas DataFrame to integers?." PSYCHOLOGICAL SCALES, 11 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-convert-all-columns-of-a-pandas-dataframe-to-integers/.
stats writer. "How can I convert all columns of a Pandas DataFrame to integers?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-convert-all-columns-of-a-pandas-dataframe-to-integers/.
stats writer (2024) 'How can I convert all columns of a Pandas DataFrame to integers?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-convert-all-columns-of-a-pandas-dataframe-to-integers/.
[1] stats writer, "How can I convert all columns of a Pandas DataFrame to integers?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I convert all columns of a Pandas DataFrame to integers?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
