Daddu
Daddu

Reputation: 1

"For loop" is working for the first variable and not for the rest of the variables in python

Why "for loop" is working for the first variable and not for the rest of the variables? I have tried the below python code for outliers:

def outliers(features, df):
    for i in features:
        Q1=df[i].quantile(0.25)
        Q3=df[i].quantile(0.75)
        IQR=Q3-Q1
        upper=Q3+(1.5*IQR)
        lower=Q1-(1.5*IQR)
        df.loc[(df[i]>upper),i] = upper 
        df.loc[(df[i]<lower),i] = lower
        return i

outliers(['Infant_Deaths','Hepatitis', 'Measles', 'Underfive_Deaths', 'Polio', 'Diphtheria', 
          'HIV', 'GDP', 'Population','Malnourished10_19','Malnourished5_9', 'Income_Index', 'Schooling'], df)

Upvotes: 0

Views: 33

Answers (1)

JRose
JRose

Reputation: 1432

Remove the return statement from your for loop. return will cause you to exit the function (and for loop)!

def outliers(features, df): 
    for i in features: 
        Q1=df[i].quantile(0.25) 
        Q3=df[i].quantile(0.75) 
        IQR=Q3-Q1 
        upper=Q3+(1.5IQR) 
        lower=Q1-(1.5IQR) 
        df.loc[(df[i]>upper),i] = upper 
        df.loc[(df[i]<lower),i] = lower

outliers(['Infant_Deaths','Hepatitis', 'Measles', 'Underfive_Deaths', 'Polio', 'Diphtheria', 'HIV', 'GDP', 'Population','Malnourished10_19','Malnourished5_9', 'Income_Index', 'Schooling'], df)

Upvotes: 0

Related Questions