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 :
import numpy as np
import pandas as pd
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 :
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 :
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 :
Last Updated :
20 May, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...