Issue of Pandas dataframe

Viewed 30

Here is my data:

df1 = pd.read_csv('gp_data.csv',sep= ";")
df2 = pd.read_csv('ms_data.csv',sep= ";")
#2.7.2
student_data = pd.concat([df1, df2])
#2.7.3 
df3 = pd.read_csv('gp_grades.csv',sep= ";")
df4 = pd.read_csv('ms_grades.csv',sep= ";")
#2.7.4
student_grades = pd.concat([df3, df4])
#2.7.5
student = pd.merge(left=student_grades, right=student_data, left_on='student_id', right_on='student_id')

For a student to pass the exam, they have to score 12 or higher in one of their G1, G2, or G3 test. With this information, do the following:

Select all the students who failed the exam. My codes are below:

crit_a1 = student.G1 = 12
crit_a2 = student.G2 = 12
crit_a3 = student.G3 = 12
final_crit_a != crit_a1 | crit_a2 | crit_a3

NameError: name 'final_crit_a' is not defined

Anyone can help with this?

1 Answers

If need assign inverted chained masks use ~ and assign by =, for compare use >= for greater or equal:

crit_a1 = student.G1 >= 12
crit_a2 = student.G2 >= 12
crit_a3 = student.G3 >= 12
final_crit_a = ~(crit_a1 | crit_a2 | crit_a3)

If change logic - get all students with less like 12 values with chained by & for bitwise AND:

crit_a1 = student.G1 < 12
crit_a2 = student.G2 < 12
crit_a3 = student.G3 < 12
final_crit_a = (crit_a1 & crit_a2 & crit_a3)

What is same like compare all selected columns with DataFrame.any for test at least one True or by DataFrame.all for test if all values are Trues per rows:

final_crit_a = ~((student[['G1','G2','G3']] >= 12).any(axis=1))

final_crit_a = (student[['G1','G2','G3']] < 12).all(axis=1)
Related