0
import pandas as pd
df = pd.read_csv (r'result_withoutTotal.csv', index_col=0) 
df['Total'] = (((5/100)*(df['Ass1']+df['Ass3']))+((15/100)*(df['Ass2']+df['Ass4']))+((60/100)*df['Exam']))


df.loc[df['Total'] > 100, 'Total'] = 100 #

df['Final'] = round(df['Total']) #rounds up the Total to the nearest integer and saves it in column Final

print(df) 

for index, row in df.iterrows():
    if row['Total'] >= 50 and row['Exam'] >= 48:
        print('Student ID:', str(index) + ' Passed')
        
    elif row['Exam'] < 48 and row['Final'] > 44:
        print('Student ID:', str(index) + ' Failed and Will have 44')
        print('')

print(df)

The Question

  • If a student failed the hurdle (Exam >= 48), the max Final is 44. No change to Final score if Final <44.

So in the For loop ( ELIF ) part i need to change any value in column 'Final' to 44 if the conditional statement is true in the main dataframe(df), I just cant figure it out, i tried row['Final] = 44 but that just changes it within the for loop.

help00621
  • 5
  • 2
  • Maybe `df[index]['Final'] = 44`? – Tomerikoo Dec 09 '20 at 14:03
  • Does this answer your question? [Set value for particular cell in pandas DataFrame using index](https://stackoverflow.com/questions/13842088/set-value-for-particular-cell-in-pandas-dataframe-using-index) – Tomerikoo Dec 09 '20 at 14:04

1 Answers1

0

row is a copy of the row in the dataframe. Modifying it does not modify the original frame.

You need to index the original frame, then you can use df.ix[index, "Final"] = 44

thshea
  • 617
  • 3
  • 14