'Create a function that will accept a DataFrame as input and return pie-charts for all the appropriate Categorical features

I can create 1 pie-chart using the 'Churn' column to group the data, however, not sure how to create a function that will accept a DataFrame as input and return pie-charts for all the appropriate Categorical features & show percentage distribution in the pie charts?

As DataFrame, I am using "Telco-Customer-Churn.csv"

f,axes=plt.subplots(1,2,figsize=(17,7))
df_churn['Churn'].value_counts().plot.pie(autopct='%1.1f%%',ax=axes[0])
sns.countplot('Churn',data=df_churn,ax=axes[1])
axes[0].set_title('Categorical Variable Pie Chart')
plt.show()


Solution 1:[1]

I did something like this, not sure if i did it right:-

#%% PlotMultiplePie

Input: df = Pandas dataframe, categorical_features = list of features , dropna = boolean variable to use NaN or not
Output: prints multiple px.pie()

def PlotMultiplePie(df_churn,categorical_features = None,dropna = False): # set a threshold of 30 unique variables, more than 50 can lead to ugly pie charts threshold = 40

# if user did not set categorical_features 
if categorical_features == None: 
    categorical_features = df_churn.select_dtypes(['object','category']).columns.to_list()
    print(categorical_features)

# loop through the list of categorical_features 
for cat_feature in categorical_features: 
    num_unique = df_churn[cat_feature].nunique(dropna = dropna)
    num_missing = df_churn[cat_feature].isna().sum()
    # prints pie chart and info if unique values below threshold 
    if num_unique <= threshold:
        print('Pie Chart for: ', cat_feature)
        print('Number of Unique Values: ', num_unique)
        print('Number of Missing Values: ', num_missing)
        fig = px.pie(df_churn[cat_feature].value_counts(dropna = dropna), values=cat_feature, 
             names = df_churn[cat_feature].value_counts(dropna = dropna).index,title = cat_feature,template='ggplot2')
        fig.show()
    else: 
        print('Pie Chart for ',cat_feature,' is unavailable due high number of Unique Values ')
        print('Number of Unique Values: ', num_unique)
        print('Number of Missing Values: ', num_missing)
        print('\n')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 shaker