text_plain_output_1.png
import matplotlib.ticker as mtick

import pandas as pd

df = pd.read_csv('../input/churn-prediction/Churn.csv')

df.shape

df.isna().sum()

df.dtypes

df['TotalCharges'] = df.TotalCharges.convert_objects(convert_numeric=True)

df['tenure_range'] = pd.cut(df.tenure, [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75], right=True)

df.dropna(inplace=True)

df = df.drop('tenure', axis=1)

gender_dis=(df['gender'].value_counts()*100/len(df)).plot(kind='bar',stacked=True)
gender_dis.set_ylabel('% Customers')
gender_dis.set_xlabel('Gender')
gender_dis.yaxis.set_major_formatter(mtick.PercentFormatter())

totals = []
for i in gender_dis.patches:
    totals.append(i.get_width())
total = sum(totals)

#to print the values on top of the bars
for i in gender_dis.patches:
    gender_dis.text(i.get_x()+.15, i.get_height()+3.9, \
            str(round((i.get_height()/total), 1))+'%',
            fontsize=12,
            color='black')

sc_dis=(df['SeniorCitizen'].value_counts()*100/len(df)).plot(kind='bar',stacked=True)
sc_dis.set_ylabel('% Customers')
sc_dis.set_xlabel('Senior Citizen')
sc_dis.yaxis.set_major_formatter(mtick.PercentFormatter())

totals = []
for i in sc_dis.patches:
    totals.append(i.get_width())
total = sum(totals)

for i in sc_dis.patches:
    sc_dis.text(i.get_x()+.15, i.get_height()+5, \
            str(round((i.get_height()/total), 1))+'%',
            fontsize=12,
            color='black')

sc_dependents=df.groupby(['SeniorCitizen','Dependents']).size().unstack()
sc_dependents=(sc_dependents.T*100/sc_dependents.T.sum()).T.plot(kind='bar',stacked=True)
sc_dependents.set_ylabel('% Customers')
sc_dependents.set_xlabel('Senior Citizens')
sc_dependents.yaxis.set_major_formatter(mtick.PercentFormatter())
for i in sc_dependents.patches:
    width, height = i.get_width(), i.get_height()
    x, y =i.get_xy() 
    sc_dependents.annotate('{:.0f}%'.format(height), (i.get_x()+.40*width, i.get_y()+.3*height),
                color = 'black')

sc_dependents = df.groupby(['Partner', 'Dependents']).size().unstack()
sc_dependents = (sc_dependents.T * 100 / sc_dependents.T.sum()).T.plot(kind='bar', stacked=True)
sc_dependents.set_ylabel('% Customers')
sc_dependents.set_xlabel('Partners')
sc_dependents.yaxis.set_major_formatter(mtick.PercentFormatter())
for i in sc_dependents.patches:
    width, height = (i.get_width(), i.get_height())
    x, y = i.get_xy()
    sc_dependents.annotate('{:.0f}%'.format(height), (i.get_x() + 0.4 * width, i.get_y() + 0.3 * height), color='black')