본문 바로가기

Data Science/Pandas

[pandas] Cut rows based on integer

To cut rows based on integer and convert its type into category, there are two method :

 

  • pd.cut() : Set boundary while we cut rows based on integer
  • pd.qcut() : Set automatic boundary while we cut rows based on integer. After using this method, final datatype of columns become Categorical class.

 

bins = [1, 20, 30, 50, 70, 100]
labels = ["미성년자", "청년", "중년", "장년", "노년"]
titanic['age_cat'] = pd.cut(titanic.age, bins = bins, labels = labels)
titanic.head()

titanic['category3'] = pd.cut(titanic.age, bins = bins, labels = labels)
titanic['category3'] = titanic.apply(lambda x : "미성년자" if x.age < 20 else titanic.category3.astype('str') + x.sex, axis = 1)
titanic.head()

'Data Science > Pandas' 카테고리의 다른 글

[pandas] Optimizing DataFrame's Memory  (0) 2022.10.11
[pandas] Introduction to Pandas  (0) 2022.10.04
[pandas] Basic Data Exploration  (0) 2022.09.19
[pandas] Useful personal function for EDA  (0) 2022.09.18
[pandas] Set options  (0) 2022.09.18