2

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?

4

3 回答 3

2

您需要创建一个名为Loan_status_word然后旋转的新列。

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')
于 2016-12-15T20:07:52.880 回答
1

尝试格式化您的数据,使您想要在图例中的每个项目都在一个列中。

df = pd.DataFrame(
    [
        [3, 1],
        [4, 1],
        [1, 4],
        [1, 3]
    ], 
    pd.MultiIndex.from_product([(1, 0), list('MF')], names=['Credit', 'Gendeer']),
    pd.Index(['Yes', 'No'], name='Loan Status')
)
df

在此处输入图像描述


然后你可以绘制

df.plot.bar(stacked=True)

在此处输入图像描述

于 2016-12-15T20:11:30.053 回答
1

下面是实现预期结果的代码:

temp4=pd.crosstab([df['Credit_History'],df['Gender']],df['Loan_Status'])
temp4.plot(kind='bar',stacked=True,color=['red','blue'],grid=False)
于 2017-03-06T11:17:55.180 回答