Python 如何使用函数在数据帧中传递特征
我创建了一个有助于绘图的函数,但使用传递的功能/列时遇到问题Python 如何使用函数在数据帧中传递特征,python,pandas,matplotlib,Python,Pandas,Matplotlib,我创建了一个有助于绘图的函数,但使用传递的功能/列时遇到问题 def default_distribution_plot(feature): # Find the years on network for those subscribers that have defaulted and not defaulted default_no = df[df['overdue']==0].feature default_yes = df[df['overdue']==1].fe
def default_distribution_plot(feature):
# Find the years on network for those subscribers that have defaulted and not defaulted
default_no = df[df['overdue']==0].feature
default_yes = df[df['overdue']==1].feature
# add comma seperator
fig, ax = plt.subplots()
ax.get_yaxis().set_major_formatter(plt.FuncFormatter(lambda x, loc: "{:,}".format(int(x))))
ax.get_xaxis().set_major_formatter(plt.FuncFormatter(lambda x, loc: "{:,}".format(int(x))))
plt.xlabel('Years on net')
plt.ylabel('Number of subscribers')
plt.title('Subscribers default visualization')
# Distribution
plt.hist([default_yes, default_no], color=['red', 'green'], label=['Default=Yes', 'Default=No'])
plt.legend()
return plt.show()
# Calling the function
feature = years_on_net # 'years_on_net ' represent one of the columns
default_distribution_plot(feature)
错误
AttributeError:“DataFrame”对象没有属性“feature”
尝试更改代码中的以下行:
default_no = df[df['overdue']==0].feature
default_yes = df[df['overdue']==1].feature
致:
并将功能作为字符串传递:
default_distribution_plot('years_on_net')
或
default_distribution_plot('years_on_net')
feature = 'years_on_net ' #represent one of the columns
default_distribution_plot(feature)