top of page

Classes More Than 5 Students

LeetCode Problem 596

Classes More Than 5 Students
Write a solution to find all the classes that have at least five students. Return the result table in any order.

import pandas as pd

data = [['A', 'Math'], ['B', 'English'], ['C', 'Math'], ['D', 'Biology'], ['E', 'Math'], ['F', 'Computer'], ['G', 'Math'], ['H', 'Math'], ['I', 'Math']]
courses = pd.DataFrame(data, columns=['student', 'class']).astype({'student':'object', 'class':'object'})

def find_classes(courses: pd.DataFrame) -> pd.DataFrame:
    class_counts = courses["class"].value_counts().reset_index()
    class_counts = class_counts[class_counts["count"]>=5]
    class_counts = class_counts[['class']]
    return class_counts    

result = find_classes(courses)
print(result)

bottom of page