Let's say I have the following dataframe:
df = pd.DataFrame([[1, np.nan, 4, 5, 6, 7], [1, 3, 2, 1, np.nan, 8], [4, 6, 1, 1, 1, 4]], columns=['Student1', 'Student2', 'Student3', 'Exam1', 'Exam2', 'Exam3'])
>>> df
Student1 Student2 Student3 Exam1 Exam2 Exam3
0 1 NaN 4 5 6.0 7
1 1 3.0 2 1 NaN 8
2 4 6.0 1 1 1.0 4
I want to sum the cells of Students and Exams only when a value in a specific column is not nan. For the first row, for the example, I could sum only Student1 & Student3 (result in column student_sum) and Exam1 and Exam3, since the Student2 is empty, Exam2 is not considered as well. I've tried to iterate using itertuples, but the problem is that it sums everything.
The results should be:
Student1 Student2 Student3 Exam1 Exam2 Exam3 Res_stud Res_exam
0 1 NaN 4 5 6.0 7 5 12
1 1 3.0 2 1 NaN 8 3 9
2 4 6.0 1 1 1.0 4 11 6