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
The following code shows how to use the astype() function to convert both the ID and tenure column to integer:
#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 here.
Additional Resources
The following tutorials explain how to perform other common conversions in pandas:
How to Convert Pandas DataFrame Columns to Strings
How to Convert Timestamp to Datetime in Pandas
How to Convert Datetime to Date in Pandas
How to Convert Strings to Float in Pandas