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)
grade    category
dtype: object

Using series.

Use dtype='category' while creating a series.
import pandas as pd
print(my_df.dtypes) # category

Using cut()

We can use cut() function to group data in bins.
import pandas as pd 
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()
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer