Advertisement
elena1234

cut method and creating bins in Python

Mar 22nd, 2023
685
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. age_bins = [0, 10, 18, 30, 55, 100]
  2. cats = pd.cut(titanic.age, age_bins, right = False)
  3. titanic["age_cat"] = cats
  4. titanic.groupby("age_cat").survived.mean()
  5.  
  6. group_names = ["child", "teenager", "young_adult", "adult", "elderly"]
  7. titanic["age_cat"] = pd.cut(titanic.age, age_bins, right = False, labels = group_names)
  8.  
  9. ########################################################################################
  10. titanic["fare_cat"] = pd.cut(titanic.fare, 5, precision= 0)
  11. titanic.fare_cat.value_counts()
  12.  
  13. ########################################################################################
  14. titanic["fare_cat"] = pd.qcut(titanic.fare, 5)
  15.  
  16. fare_labels =["very_cheap", "cheap", "moderate", "exp", "very_exp"]
  17. titanic["fare_cat"] =  pd.qcut(titanic.fare, [0, 0.1, 0.25, 0.5, 0.9, 1], precision = 0, labels = fare_labels)
  18. titanic.fare_cat.value_counts()
  19. titanic.groupby(["age_cat", "fare_cat"]).survived.mean().unstack()
  20.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement