Q#5: Filtering Student Information with Pandas

Abish Pius
1 min readSep 5, 2020

Write code using Python Pandas to select the rows where the students’ favorite color is blue or yellow and their grade is above 90.

-erik@interviewqs.com

TRY it yourself!

ANSWER:

This question requires you to use Python’s Panda’s package as a data table/frame querying tool similar to SQL. To answer recall your simple logical statements and how to query a Pandas dataframe object. The pd.loc[] function allows querrying dataframe rows based on logical statements.

import pandas as pdraw_data = {'name': ['Willard Morris', 'Al Jennings', 'Omar Mullins', 'Spencer McDaniel'],'age': [20, 19, 22, 21],'favorite_color': ['blue', 'blue', 'yellow', "green"],'grade': [88, 92, 95, 70]}df = pd.DataFrame(raw_data)df.loc[(df['favorite_color'] != 'green') & (df['grade'] > 90)]

--

--

Abish Pius

Data Science Professional, Python Enthusiast, turned LLM Engineer