Categorical Data Types in Pandas

Pandas dtypes

Creating categorical data types

Using astype()
import pandas as pd
my_dict={'grade':['a', 'c', 'b', 'b','b','c']} # dictionary 
my_data = pd.DataFrame(data=my_dict)
my_data['grade']=my_data['grade'].astype('category')
print(my_data.dtypes)
output
grade    category
dtype: object

Using series.

Use dtype='category' while creating a series.
import pandas as pd
my_df=pd.Series(['d','a','b','c','a','c'],dtype='category')
print(my_df.dtypes) # category

Using cut()

We can use cut() function to group data in bins.
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
         'ID':[1,2,3,4,5,6],
         'MATH':[80,40,70,70,60,30],
         'ENGLISH':[80,70,40,50,60,30]}
my_data = pd.DataFrame(data=my_dict)
my_data['my_cut'] = pd.cut(x=my_data['MATH'],bins=[1, 50, 70, 100]) 
print(my_data['my_cut'].dtypes) # category

Pandas Data Cleaning dtypes()
to_timedelta() select_dtypes() timedelta64()


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer