Data Science/Pandas

[pandas] Cut rows based on integer

See_the_forest 2022. 9. 18. 15:40

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()