Open In App

Python | Pandas Categorical DataFrame creation

Last Updated : 20 May, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

pandas.DataFrame(dtype=”category”) : For creating a categorical dataframe, dataframe() method has dtype attribute set to category.
All the columns in data-frame can be converted to categorical either during or after construction by specifying dtype=”category” in the DataFrame constructor.

Code :




# Python code explaining
# constructing categorical data frame
   
# importing libraries
import numpy as np
import pandas as pd
  
# Constructing dataframe 
data = {'col1': [1, 2, 4, 5], 'col2': [3, 4, 5, 6]}
df1 = pd.DataFrame(data = data)
  
print ("df1 : \n", df1)
print("\n\ndf1 type :\n", df1.dtypes)


Output :

 




# Converting dataframe to category
df2 = pd.DataFrame({'A': list('1245'), 'B': list('3456')}, dtype ="category")
  
print ("df2 : \n", df2)
print("\n\ndf2 type :\n", df2.dtypes)
  
print ("\n\ndf2 column 0 :\n", df2['A'])
print ("\n\ndf2 column 1 :\n", df2['B'])


Output :

 




# Conversion can be done using astype()
df3 = pd.DataFrame({'A': list('efgh'), 'B': list('aebc')})
print ("\n\ndf3 : \n", df3)
print("\ndf3 type :\n", df3.dtypes)
  
df4 = df3.astype('category')
print ("\n\ndf4 type:\n", df4.dtypes)


Output :



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads