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.

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

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

GitFlow with GitHub

The eye and The programmer

Enterprise Search Solutions — Ontology, Knowledge Graph & Semantic Search

HTTP Request Smuggling: Part-2 (Identify & Exploit)

Connect an Autonomous JS App to ActionCable for Realtime Goodness

Setting up LAMP stack on AWS EC2

How to use volumes to make dynamic changes to code running in containers

Understanding delegate in Ruby on Rails

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

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

More from Medium

List Comprehension in Python

Python data cleaning basics part 2

What is Function in Python? How To Create User-Defined Function?

Data Processing in Python