Q#5: Filtering Student Information with Pandas

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

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)]

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abish Pius

Abish Pius

34 Followers

Data Science Professional who teaches with unflashy, simple to understand python code.