简体   繁体   English

熊猫没有绘制数据透视表输出?

[英]Pandas not plotting pivot table output?

I have a data frame with variables Credit_History (0 or 1) and Loan_Status (Y or N). 我有一个带有变量Credit_History(0或1)和Loan_Status(Y或N)的数据框。 Temp1 just shows my count of rows labeled 0 or 1. Temp2 is where I want to code the Y or N to 0 or 1, then average it. 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)

When I plot, I would expect a 1 row by 2 column area. 当我绘图时,我期望1行2列的面积。 But it looks like a 2 row by 2 column area that contains 3 plots. 但是它看起来像一个2行2列的区域,包含3个图。 Temp2 is being plotted 2 times, but one is blank other than axis titles. Temp2被绘制了2次,但除了轴标题外,其余为空白。 I'm assuming I'm declaring something wrong when I'm creating the temp2 object... enter image description here 我假设我在创建temp2对象时声明了错误... 在此处输入图像描述

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')

Consider laying out the dimensions of plot and then assign axes to pandas plots: 考虑布局图的尺寸,然后将轴分配给熊猫图:

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()

Output (using random data) 输出 (使用随机数据)

绘图输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM