Python – KeyError: 0 Trying to create a new column in a dataframe

Question:

I am trying to create a new column in a data frame with somes conditionals with a function, but when i run the code, i have the KeyError: 0.
This is my code:


    def createApproved(df):
        df['approved'] = np.nan
        for i in range(len(df)):
            if df['extra'][i] < 0.8:
                df['approved'] = 0
                
            elif df['G3'][i] < 10:
                df['approved'] = 0
            
            elif df['G3'][i] <= 15  and df['G3'][i] >= 10:
                df['approved'] = 1
            
            elif df['G3'][i] > 15:
                df['approved'] = 1
                df['extra'][i] = 0
    
    createApproved(GabrielPereira_mat)
    createApproved(GabrielPereira_por)
    createApproved(Mousinho_mat)
    createApproved(Mousinho_por)

Answers:

You can avoid loops and set values by conditions chained by | for bitwise OR in numpy.select and numpy.where :

 def createApproved(df):
        m1 = df['extra'] < 0.8
        m2 = df['G3'] < 10
        m3 = df['G3'].between(10,15)
        m4 = df['G3'] > 15
        
        df['approved'] = np.select([m1 | m2 | m4, m3], [0,1], default=None)
        df['extra'] = np.where(m4, 0, df['extra'])
        
        return df
        
Answered By: jezrael
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.