Shadow Walker
Shadow Walker

Reputation: 1199

How to pass a feature in pandas dataframe using a function

I have created a function that will help in plotting but i'm having a problem using the passed feature/column

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)

Error

AttributeError: 'DataFrame' object has no attribute 'feature'

Upvotes: 0

Views: 45

Answers (1)

Kay
Kay

Reputation: 2332

Try changing the following lines in your code:

default_no = df[df['overdue']==0].feature
default_yes = df[df['overdue']==1].feature

to:

default_no = df.loc[df['overdue']==0, feature]
default_yes = df.loc[df['overdue']==1, feature]

And pass feature as a string:

default_distribution_plot('years_on_net')

or

feature = 'years_on_net ' #represent one of the columns
default_distribution_plot(feature)

Upvotes: 1

Related Questions