Thursday, July 4, 2024
HomeLanguagesPythonPython | Pandas Categorical DataFrame creation

Python | Pandas Categorical DataFrame creation

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 :

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments