Conditional looping: Pandas Python -


question regarding conditional looping on pandas dataframe. data frame of interest huge. have student name(s) , test score(s) @ different time in columns (please see below). student considered fail if his/her score less 75 in of tests, pass otherwise. i'm not able efficiently. dataframe:

score = {'student_name': ['jiten', 'jac', 'ali', 'steve', 'dave', 'james'],     'test_quiz_1': [74, 81, 84, 67, 59, 96], 'test_quiz_2': [76, np.nan, 99, 77, 53, 69], 'test_mid_term': [76, 88, 84, 67, 58, np.nan], 'test_final_term': [76, 78, 89, 67, 58, 96]}  df = pd.dataframe(score, columns = ['student_name', 'test_quiz_1', 'test_quiz_2', 'test_mid_term', 'test_final_term']) 

my approach: (modifying based on jacques kvam's answer)

df.test_quiz_1 > 70 

this(^) gives me location particular student fail. same can repeated other tests (df.test_quiz_2, ...). finally, need combine these 1 final column student failed if he/she fails @ test.

edited: have little knowledge python , pandas. i'm writing pseudo code how have implemented in c/c++.

for student in student_list:     value=0     in range (no_of_test):         if (score<75):              value=value+1         else:              continue     if(value>0):          student[status]=fail     else:          student[status]=pass 

above pseudo code. i'm not creating additional column mark if student fail in test or not. possible implement similar in python using pandas.

please advice.

i think suits needs:

cols = df.columns.drop("student_name").tolist() df["passorfail"] = df[cols].fillna(0).lt(75).any(1)  in cols:     df[i+"_"] = df[i].fillna(0).lt(75) 

explanation

first create list relevant columns:

['test_quiz_1', 'test_quiz_2', 'test_mid_term', 'test_final_term'] 

we create new col ["passorfail”] checks if dataframe conataining relevant columns (np.nan=0) lower 75.

and lastly create new column every relevant column true or false values.

update

let's interested in getting true or false, following code should sufficient:

cols = df.columns.drop("student_name").tolist() results = df[cols].fillna(0).lt(75).any(1).tolist() (~pd.series(results,index=df["student_name"])).to_dict() 

outputs:

{'ali': true, 'dave': false, 'jac': false, 'james': false, 'jiten': false, 'steve': false} 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -