Search code examples
pythonpython-3.xpandasdata-munging

Why won't barchart in Pandas stack different values?


Using Pandas, python 3. Working in jupyter.

Ive made this graph below using the following code:

temp3 = pd.crosstab(df['Credit_History'], df['Loan_Status']) 
temp3.plot(kind = 'bar', stacked = True, color = ['red', 'blue'], grid = False)
print(temp3)

Stacked Bars

And then tried to do the same, but with divisions for Gender. I wanted to make this: Four Bars

So I wrote this code: enter image description here

And made this monstrosity. I'm unfamiliar with pivot tables in pandas, and after reading documentation, am still confused. I'm assuming that aggfunc affects the values given, but not the indices. How can I separate the loan status so that it reads as different colors for 'Y' and 'N'?

Trying a method similar to the methods used for temp3 simply yields a key error:

temp3x = pd.crosstab(df['Credit_History'], df['Loan_Status', 'Gender']) 
temp3x.plot(kind = 'bar', stacked = True, color = ['red', 'blue'], grid = False)
print(temp3)

How can I make the 'Y' and 'N' appear separately as they are in the first graph, but for all 4 bars instead of using just 2 bars?


Solution

  • You need to make a new column called Loan_status_word and then pivot.

    loan_status_word = loan_status.map({0:'No', 1:'Yes'})
    df.pivot_table(values='Loan_Status', 
                   index=['Credit_History', 'Gender'], 
                   columns = 'loan_status_word', 
                   aggfunc ='size')