Table of Contents
Changing column type in Pandas is a process that allows users to convert the data type of a specific column in a Pandas DataFrame. This can be done using the “astype()” function, which allows users to specify the desired data type for the column. This process is useful in situations where the current data type of a column does not accurately represent the data or when performing certain operations that require a specific data type. By following the steps to change column type in Pandas, users can easily manipulate and manage their data more effectively.
Change Column Type in Pandas (With Examples)
Columns in a pandas DataFrame can take on one of the following types:
- object (strings)
- int64 (integers)
- float64 (numeric values with decimals)
- bool (True or False values)
- datetime64 (dates and times)
The easiest way to convert a column from one data type to another is to use the astype() function.
You can use the following methods with the astype() function to convert columns from one data type to another:
Method 1: Convert One Column to Another Data Type
df['col1'] = df['col1'].astype('int64')
Method 2: Convert Multiple Columns to Another Data Type
df[['col1', 'col2']] = df[['col1', 'col2']].astype('int64')
Method 3: Convert All Columns to Another Data Type
df = df.astype('int64')
The following examples show how to use each method in practice with the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'ID': ['1', '2', '3', '4', '5', '6'], 'tenure': [12.443, 15.8, 16.009, 5.06, 11.075, 12.9546], 'sales': [5, 7, 7, 9, 12, 9]}) #view DataFrame print(df) ID tenure sales 0 1 12.4430 5 1 2 15.8000 7 2 3 16.0090 7 3 4 5.0600 9 4 5 11.0750 12 5 6 12.9546 9 #view data type of each column print(df.dtypes) ID object tenure float64 sales int64 dtype: object
Example 1: Convert One Column to Another Data Type
The following code shows how to use the astype() function to convert the tenure column from a float to an integer:
#convert tenure column to int64
df['tenure'] = df['tenure'].astype('int64')
#view updated data type for each column
print(df.dtypes)
ID object
tenure int64
sales int64
dtype: object
Notice that the tenure column has been converted to int64 while all other columns have retained their original data type.
Example 2: Convert Multiple Columns to Another Data Type
#convert ID and tenure columns to int64
df[['ID', 'tenure']] = df[['ID', 'tenure']].astype('int64')
#view updated data type for each column
print(df.dtypes)
ID int64
tenure int64
sales int64
dtype: object
Notice that both the ID and tenure columns have been converted to int64.
Example 3: Convert All Columns to Another Data Type
The following code shows how to use the astype() function to convert all columns in the DataFrame to an integer data type:
#convert all columns to int64
df = df.astype('int64')
#view updated data type for each column
print(df.dtypes)
ID int64
tenure int64
sales int64
dtype: object
Notice that all columns have been converted to int64.
Note: You can find the complete documentation for the pandas astype() function .
The following tutorials explain how to perform other common conversions in pandas:
Cite this article
stats writer (2024). How to change column type in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-to-change-column-type-in-pandas/
stats writer. "How to change column type in Pandas?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-to-change-column-type-in-pandas/.
stats writer. "How to change column type in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-to-change-column-type-in-pandas/.
stats writer (2024) 'How to change column type in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-to-change-column-type-in-pandas/.
[1] stats writer, "How to change column type in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How to change column type in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
