Advertisement
elena1234

cut method and creating bins in Python

Mar 22nd, 2023 (edited)
753
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.44 KB | Source Code | 0 0
  1. plt.style.use("ggplot")
  2. plt.figure(figsize=(12, 8))
  3. plt.bar(df_converted['Year'], df_converted['Dividends'], color = 'b')
  4.  
  5. # Set x-axis ticks to display every year between 1 and rotate them
  6. plt.xticks(np.arange(df_converted['Year'].min(), df_converted['Year'].max() + 1, 1), rotation=45)
  7.  
  8. plt.xlabel('Year')
  9. plt.ylabel('Total Dividends')
  10. plt.title('Dividends by Year')
  11.  
  12. # Show grid and plot
  13. plt.grid(True)
  14. plt.show()
  15.  
  16. ########################################################################################################3
  17. age_bins = [0, 10, 18, 30, 55, 100]
  18. cats = pd.cut(titanic.age, age_bins, right = False)
  19. titanic["age_cat"] = cats
  20. titanic.groupby("age_cat").survived.mean()
  21.  
  22. group_names = ["child", "teenager", "young_adult", "adult", "elderly"]
  23. titanic["age_cat"] = pd.cut(titanic.age, age_bins, right = False, labels = group_names)
  24.  
  25. ########################################################################################
  26. titanic["fare_cat"] = pd.cut(titanic.fare, 5, precision= 0)
  27. titanic.fare_cat.value_counts()
  28.  
  29. ########################################################################################
  30. titanic["fare_cat"] = pd.qcut(titanic.fare, 5)
  31.  
  32. fare_labels =["very_cheap", "cheap", "moderate", "exp", "very_exp"]
  33. titanic["fare_cat"] =  pd.qcut(titanic.fare, [0, 0.1, 0.25, 0.5, 0.9, 1], precision = 0, labels = fare_labels)
  34. titanic.fare_cat.value_counts()
  35. titanic.groupby(["age_cat", "fare_cat"]).survived.mean().unstack()
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement