Search code examples
pythonpandasmatplotlib

Plotting a matplotlib pie chart using 2 columns


I'm using this dataset and I want to plot a pie chart using matplotlib to show the percentage of males and females who have their loans approved i.e, Loan_Status == Y.

plot.pie() only takes x and labels for the data, so, I'm not able to figure how I should involve the gender column and where I should filter the Ys from the Loan_Status column.

I've tried this:

plt.pie(dataset[['Loan_Status', 'Gender']].value_counts().values, labels=['Male Approved', 'Male Denied', 'Female Approved', 'Female Denied'], colors=['#00db92', '#0049db'], autopct='%1.1f%%')
plt.title('Loan Approval by Gender')
plt.show()

Which shows this output

enter image description here

But I only want the approvals not the denials.


Solution

  • If you only want the approvals, then you should only use the rows with "Y".

    dataset[dataset["Loan_Status"] == 'Y']["Gender"].value_counts().values