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

--

--