Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用函数在数据帧中传递特征_Python_Pandas_Matplotlib - Fatal编程技术网

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)