当前位置:网站首页>Introduction to classification data cotegory and properties and methods of common APIs

Introduction to classification data cotegory and properties and methods of common APIs

2022-06-26 04:50:00 I am a little monster

pandas There is a classification data type in category, Categorical data types have the following advantages :

(1) Storing data in this way saves more memory , Increase speed , Especially when the data set contains a lot of duplicate strings

(2) When a bad quality exists in a certain order ( Like the Likert scale ) when , It should be converted into classified data

(3) There are some python The library can handle classified data ( For example, fitting statistical models )

Use astype Convert data to catecory For data types, refer to astype Convert data type _ I am a little monster blog -CSDN Blog

  The reference tables given below give the possible reclassifications series Actions performed on , Below the table are examples of attribute methods in the table

classification API
Properties or methods explain
Series.cat.categories Category
Series.cat.ordered Whether the categories are in order
Series.cat.codes Return the certificate code of the category
Series.cat.rename_categories() Rename category
Series.cat.reorder_categories() Reorder categories
Series.cat.add_categories() Add a new category
Series.cat.remove_categories() Delete category
Series.cat.remove_unused_categories() Delete unused categories
Series.cat.set_categories() Set new category
Series.cat.as_order() Sort categories
Series.cat.as_unordered() Is the category out of order
import pandas as pd 
import seaborn as sns

tips=sns.load_dataset('tips')
t=tips.head(10)
t.loc[[1,4,7],'total_bill']='missing'
print(t.dtypes)

print(t['sex'].cat.categories)

The output is as follows :

total_bill      object
tip            float64
sex           category
smoker        category
day           category
time          category
size             int64
dtype: object
------------------------------------------------
Index(['Male', 'Female'], dtype='object')
[Finished in 1.7s]

原网站

版权声明
本文为[I am a little monster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180510153672.html