Home »
Python »
Python Programs
Pandas: Changing some column types to categories
Learn, how to change some column types to categories in Python pandas?
By Pranit Sharma Last updated : October 06, 2023
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
Categorical data
Categorical data is a type of data that has some certain category or characteristic, the value of categorical data is not a single value, rather it consists of classified values, for example, an email can be considered spam or not spam, if we consider 1 as spam and 0 as not spam, we have a classified data in the form of 0 or 1, this is called categorical data.
Changing some column types to categories
To change some columns types to categories, we will loop over the specific columns of dataframe and convert their dtype using astype() method (casts a pandas object to a specified dtype dtype.).
Let us understand with the help of an example,
Python program to change some column types to categories in pandas
# Import pandas
import pandas as pd
# Creating a dataframe
df = pd.DataFrame({
'A': [1,2,3,4],
'B':[5,6,7,8],
'C':[9,10,11,12],
'D':[13,14,15,16],
'E':[17,18,19,20]
})
# Display original dataframe
print("Original DataFrame:\n",df,"\n")
# Display original data types
print("dtypes:\n",df.dtypes,"\n")
# Converting columns of df to category
for col in ['B','C','D']:
df[col] = df[col].astype('category')
# Display dtypes of new dataframe
print("New dtypes:\n",df.dtypes)
Output
The output of the above program is:
Python Pandas Programs »