为什么 Pandas 中的条形图不会堆叠不同的值?

Why won't barchart in Pandas stack different values?

使用Pandas,python 3. 在 jupyter 中工作。

我使用以下代码制作了这张图表:

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

然后尝试做同样的事情,但对性别进行划分。我想这样做:

所以我写了这段代码:

并制造了这个怪物。我不熟悉 pandas 中的数据透视表,在阅读文档后,我仍然感到困惑。我假设 aggfunc 影响给定的值,但不影响索引。我如何区分贷款状态,以便 'Y' 和 'N' 显示为不同的颜色?

尝试使用类似于 temp3 的方法只会产生一个关键错误:

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

如何使 'Y' 和 'N' 像在第一张图中一样分开显示,但对于所有 4 个柱而不是仅使用 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')

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

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)

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

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