【问题标题】:Pandas not plotting pivot table output?熊猫没有绘制数据透视表输出?
【发布时间】:2017-11-23 12:14:44
【问题描述】:

我有一个包含变量 Credit_History(0 或 1)和 Loan_Status(Y 或 N)的数据框。 Temp1 只显示我标记为 0 或 1 的行数。Temp2 是我要将 Y 或 N 编码为 0 或 1 的位置,然后对其进行平均。

temp1 = df['Credit_History'].value_counts(ascending=True)
temp2 = df.pivot_table(values='Loan_Status',index=
['Credit_History'],aggfunc=lambda x: x.map({'Y':1,'N':0}).mean())

print ('Frequency Table for Credit History:')
print (temp1)
print ('\nProbility of getting loan for each Credit History class:') 
print (temp2)

当我绘图时,我希望有一个 1 行 x 2 列的区域。但它看起来像一个包含 3 个图的 2 行 x 2 列区域。 Temp2 被绘制了 2 次,但除了轴标题之外,其中一个是空白的。我假设我在创建 temp2 对象时声明了一些错误...enter image description here

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(12,4))
ax1 = fig.add_subplot(121)
ax1.set_xlabel('Credit_History')
ax1.set_ylabel('Count of Applicants')
ax1.set_title('Applicants by Credit_History')
temp1.plot(kind='bar')


ax2 = fig.add_subplot(122)
temp2.plot(kind = 'bar')
ax2.set_xlabel('Credit_History')
ax2.set_ylabel('Probability of getting loan')
ax2.set_title('Probability of getting loan by credit history')

【问题讨论】:

    标签: python pandas plot pivot pivot-table


    【解决方案1】:

    考虑布置图的尺寸,然后将轴分配给熊猫图:

    fig, axs = plt.subplots(nrows = 1, ncols=2, figsize=(12,4))
    
    temp1.plot(kind='bar', title='Applicants by Credit_History', ax=axs[0])             
    axs[0].set_xlabel('Credit_History')
    axs[0].set_ylabel('Count of Applicants')
    
    temp2.plot(kind = 'bar', title='Probability of getting loan by credit history', ax=axs[1])
    axs[1].set_xlabel('Credit_History')
    axs[1].set_ylabel('Probability of getting loan')
    
    fig.tight_layout()
    plt.show()
    plt.clf()
    plt.close()
    

    输出 (使用随机数据)

    【讨论】:

      猜你喜欢
      • 2018-09-08
      • 2019-03-03
      • 2021-02-28
      • 2023-01-11
      • 1970-01-01
      • 1970-01-01
      • 2017-01-14
      • 2019-04-28
      相关资源
      最近更新 更多