mercredi 25 novembre 2020

How to return a dataframe column values from a For loop

I'm trying to calculate the value of one column based on the entries of another column, I'm trying to achieve this using for loop, I don't know how to return the values for entire column, rather just the last iteration, below is an example of my code, please help me find where I'm doing wrong.

def marks_calculation(Master_Sheet):
    for a in Master_Sheet.Marks Condition:
        if a == "Yes":
            Master_Sheet['Total'] = Master_Sheet['Total Marks']*0.85
            Master_Sheet.loc[(Master_Sheet['Total Marks'] > Master_Sheet['Basic1']) & (Master_Sheet['Total Marks'] <= Master_Sheet['Basic3']),'Total'] = (Master_Sheet['Basic1'] * Master_Sheet['Slab1']) + ((Master_Sheet['Total Marks'] - Master_Sheet['Basic1']) * Master_Sheet['Slab2'])
            Master_Sheet.loc[(Master_Sheet['Total Marks'] > Master_Sheet['Basic2']),'Total'] = (Master_Sheet['Basic1'] * Master_Sheet['Slab1']) + ((Master_Sheet['Basic2'] - Master_Sheet['Basic1']) * Master_Sheet['Slab2']) + ((Master_Sheet['Total Marks'] - Master_Sheet['Basic2']) * Master_Sheet['Slab3'])
            return Master_Sheet['Total']
        else :     
            Master_Sheet['Total'] = Master_Sheet['Aggregated Marks']
            return Master_Sheet['Total']
Master_Sheet['Total'] = Master_Sheet['Total'].replace("",0, regex=True)  
Master_Sheet[['Students','Total Marks','Aggregated Marks','Marks Condition','Total']]

Aucun commentaire:

Enregistrer un commentaire