Search code examples
pythonpandasfunctiondataframepandas-apply

No axis named Stats for object type DataFrame


I am trying to reuse the defined function for multiple rows in a DataFrame. But I am getting an error:

No axis named Stats for object type DataFrame

d = {'Person': ['A', 'B', 'C', 'D', 'E', 'F'],'Stats':[17, 117, -52, 100, -6, 101]}
ages = pd.DataFrame(data=d)

def my_jr(amount):
if amount > 0:
    print('amount greater than zero')
elif amount < 0:
    print('amount lesser than zero')
else:
    pass
ages.apply(my_jr,'Stats')

Solution

  • The issue here is the argument you're passing to Panda's apply method is not as expected.

    The second argument (its actually a keyword argument) is the axis -- either 0 or 1. From the documentation:

    Objects passed to the function are Series objects whose index is either the DataFrame's index (axis=0) or the DataFrame's columns (axis=1).

    So, in this case, you should be passing 1 as the axis. Each argument to your function my_jr would be a Pandas Series object, which you can then index at Stats to get the numerical value to compare.

    Here is a corrected version:

    import pandas as pd
    
    d = {
        'Person': ['A', 'B', 'C', 'D', 'E', 'F'],
        'Stats': [17, 117, -52, 100, -6, 101]
    }
    
    ages = pd.DataFrame(data=d)
    
    def my_jr(series):
        amount = series['Stats']
        if amount > 0:
            print('amount greater than zero')
        elif amount < 0:
            print('amount lesser than zero')
        else:
            pass
    
    ages.apply(my_jr, 1)
    

    Which gives the expected output:

    amount greater than zero
    amount greater than zero
    amount lesser than zero
    amount greater than zero
    amount lesser than zero
    amount greater than zero
    

    This isn't a surprising misconception with how Panda's apply works. I suggest reading more examples of it in action to understand how to use it correctly.