Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting Bar Graph according to Group

I have this Telco-Customer Churn dataset. After analyzing the churn rate according to different duration of tenure, I would like to visualize it like how it looked in the following figure, where only the number of churns is plotted against different 'bins' of tenure.

enter image description here

Below is what I have tried:

import pandas as pd
import matplotlib.pyplot as plt

user_data = pd.read_csv("https://github.com/WedamN/Telco-Churn-Prediction/blob/master/CustomerChurnData.csv")

# bin the tenure into every 6 months
user_data['tenure_bin'] = pd.cut(user_data['Tenure'], list(range(0, 73, 6)))

# some basic analysis
churn_rate_according_to_tenure = user_data.groupby('tenure_bin').Churn.value_counts('Yes')*100

# plot the results
churn_rate_according_to_tenure.plot().bar()
plt.show()

Here is the plot I obtained (which is kinda messy) where the 'Yes' and 'No' categories are showed. How can I fix this where I would only want to show only the 'Yes' category with the bars having the same color? enter image description here

like image 622
jacky_learns_to_code Avatar asked Dec 16 '25 18:12

jacky_learns_to_code


1 Answers

I think you need reshape if want both categories together by unstack:

print (churn_rate_according_to_tenure.unstack())
Churn              No        Yes
tenure_bin                      
(0, 6]      46.666667  53.333333
(6, 12]     64.113475  35.886525
(12, 18]    67.700730  32.299270
(18, 24]    75.420168  24.579832
(24, 30]    78.190255  21.809745
(30, 36]    78.553616  21.446384
(36, 42]    78.100264  21.899736
(42, 48]    83.812010  16.187990
(48, 54]    83.809524  16.190476
(54, 60]    87.378641  12.621359
(60, 66]    90.712743   9.287257
(66, 72]    94.703390   5.296610

churn_rate_according_to_tenure.unstack().plot.bar()

If want filter, select category - column:

churn_rate_according_to_tenure.unstack()['Yes'].plot.bar()
like image 153
jezrael Avatar answered Dec 19 '25 07:12

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!