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 :

filter_none

edit
close

play_arrow

link
brightness_4
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)

chevron_right


Output :



 

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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'])

chevron_right


Output :

 

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output :



My Personal Notes arrow_drop_up

Aspire to Inspire before I expire

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ManasChhabra2