Trying to create--in Python using multiple data frames--the equivalent of a countifs in Excel that would span multiple sheets.
I need a new column count of records on another data frame based on criteria from the current data frame.
See Excel impression of what I want to do in python, also here.
My goal?
- Count exams on students data frame
- by Student ID
- with exam date >= enroll date
- with exam date <= detail date
- with exam grade >= 70
Basically the Excel equivalent would be...
=COUNTIFS(Summary!$B$1:$B$11, ">="&Detail!B2, Summary!$B$1:$B$11, "<="&Detail!C2, Summary!$C$1:$C$11, ">="&70, Summary!$A$1:$A$11, "="&Detail!A2)
...where Summary is the primary data frame and Detail is the secondary data frame where I want to count records.
Found these answers in my research:
- sumifs function in python
- What is a good way to do countif in Python
- Python Pandas counting and summing specific conditions
Not quite what I'm looking for, because they don't span multiple data frames. I was able to create a basic countifs for a singular data frame:
sum(1 for x in students['Student ID'] if x == 1)
sum(1 for x in exams['Exam Grade'] if x >= 70)