Q#11: Categorizing Food

You are given the following dataframe and are asked to categorize each food into 1 of 3 categories: meat, fruit, or other.

Given this, write code to add a new column categorizing each row. Free practice through colab : DDI_q55.ipynb — Colaboratory (google.com).

-Question from erik@interviewqs.com, subscribe for free interview questions

TRY IT YOURSELF!

Answer:

This question tests your understanding of Pandas for dataframe manipulation. The first step is to create the dataframe object:

import pandas as pdraw_data = {'food': ['bacon', 'STRAWBERRIES', 'Bacon', 'STRAWBERRIES', 'BACON', 'strawberries', 'Strawberries', 'pecans'],'pounds': [4, 3.5, 7, 3, 6, 8, 1, 3]}data = pd.DataFrame(raw_data, columns = ['food', 'pounds'])

Now there are multiple ways to add columns to a dataframe in Pandas, I will use the most simple method which is just declaring a new column name as an index in your pandas object and setting it equal to a list/array of your desired results.

Before this, lets create the conditionals, note the differences in spelling of the food objects this is a common trap and it requires that you know some string manipulation in python. In this case, we are going to force all the characters to be lowercase with the method .lower(). Let us make a list to store the results and then add the list to the dataframe.

category = []
for i in data['food']:
if i.lower() == 'bacon':
category.append('meat')
elif i.lower() == 'strawberries':
category.append('fruit')
else:
category.append('other')
data['category'] = category

--

--

--

Data Science tutorial working through solutions to Data Science Interview Questions

Recommended from Medium

Using Apple’s Financial News to Predict its Stock Price Movement

Machine learning from absolute scratch: Learn Python, pandas, and use scikit-learn to make your…

In the Midst of it All: COVID-19 Pandemic Data Analysis

Not always predictive analytics is required, Exploratory Analysis can do wonders

Rats in the City Pt. 2 — Data Exploration & Analysis

Why Data Science?

Predicting large text data with spark via the R package sparklyr

Found This Week #68

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

Beginner’s Basic Python for Data Science

Difference between List, Tuple, Dictionary and Set in Python

How to Deal with Null, N/A, or Empty Cells in Your Dataframe Using Python.

Two Ways to Investigate Missing Data