FAQ: Modifying DataFrames - Review

This community-built FAQ covers the “Review” exercise from the lesson “Modifying DataFrames”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Science

Data Analysis with Pandas

FAQs on the exercise Review

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

why cant i populate the ‘shoe_source’ column with this function?
orders[‘shoe_source’]= orders.shoe_material.apply(lambda x: ‘animal’ if orders.shoe_material == ‘leather’ else ‘vegan’)

nothing happens!

1 Like

you are using ‘lambda x’ but not using x in the lambda function. the lambda is being applied to each value in orders.shoe_material already (orders.shoe_material.apply) so you need to reference x in the lambda instead of the whole orders.shoe_material series.

lambda x: ‘animal’ if x == ‘leather’ else ‘vegan’

How come that the usage of \ is very inconsistent in this lesson. It is really confusing to understand where one needs to place .
for instance: In the hint it directs us to place it after (OUTCOME IF TRUE)

In the solution it is placed before the (OUTCOME IF TRUE)

Can someone elaborate why it is like this?


Hello everyone,

I’m a bit confused about the differences in usage of ‘apply’ function in dataframes.
Say, how would each of orders.shoe_material.apply(…) and orders.apply(…) work out?


I wouldn’t call them needed.

You can use backslash to escape newline. In most cases you’re better off surrounding your expression by parentheses.

In that particular case I wouldn’t use lambda at all. Use a normal function statement instead, it’ll do the same thing.

1 Like

I was wondering why the solution doesn’t use the hint format in this module. The code is not readable and doesn’t fit the entire rhetoric of this module telling us to define our lambda equations first, then put them into the functional part of the code.


The interface seems to throw an error message when slashes aren’t used despite them not being necessary. I tried two different approaches and confirmed both work by printing the first 20 rows.

<orders['shoe_source'] = orders.shoe_material.apply(lambda x: 'animal' if x == 'leather' else 'Vegan')>

<orders['shoe_source'] = orders.shoe_material.apply(lambda x: 'Vegan' if x != 'leather' else 'animal')>

Also, for exercise #3, I literally copied the recommended code in the hint at one point and still got an error message for that line. The solution code makes a little more sense to me since it accomplishes the task in one step instead of having to define a function and then apply that function. I just wish there was more continuity in the approach throughout the modules and that the focus was always on the way to accomplish the task with the cleanest code possible.

My answer to the question 3 was okay:

salutation = lambda x: 'Dear Mr. '+ x.last_name
if x.gender == ‘male’
else 'Dear Ms. '+ x.last_name
orders[‘salutation’] = orders.apply(salutation, axis=1)

but it is creating a new DataFrame ¿what if i wanted to modify the existing one?
I tried the following code but it didn’t work:

salutation = lambda x: 'Dear Mr. '+ x.last_name
if x.gender == ‘male’
else 'Dear Ms. '+ x.last_name
orders[‘salutation’] = orders.apply(salutation, axis=1, inplace = True)

I had the same question

A question:
To solve the exercise: Using the columns last_name and gender create a column called salutation which contains Dear Mr. <last_name> for men and Dear Ms. <last_name> for women.

I came out with the following solution:

orders[‘salutation’] = 'Dear ’ + orders[‘gender’].apply(lambda x: 'Mr. ’ if x == ‘Male’ else 'Ms. ') + orders[‘last_name’]

However it didn’t work out as planned. All the strings in the column are all started with ‘Dear Ms.’ instead of ‘Dear Ms.’ and ‘Dear Mr.’ separately.

Looking forward to answers to my puzzle :sweat_smile:

Can someone explain to me why this is not working:

orders[“shoe_source”] = lambda row: “animal” if row[“shoe_material”] == “leather” else “vegan”

I think that I fixed it myself:
orders[“shoe_source”] = orders.apply(lambda row: “animal” if row[“shoe_material”] == “leather” else “vegan”, axis = 1)

you really need to be able to go back and look at your code after you ‘show solution’ so i can know wtf i did wrong. currently it just deletes what you wrote, no undo or back


‘Male’ ‘male’ another capitalization error in your example. This module is a shambles


import codecademylib
import pandas as pd
orders = pd.read_csv(‘shoefly.csv’)
orders[‘shoe_source’] = orders.apply(lambda x: ‘animal’ if x.shoe_material == ‘leather’ else ‘vegan’)
print orders

gives me


your website is broken. it’s printing code that i deleted like 5 minutes ago and it won’t update to my current code. Refreshed the page, says connected but now:

import codecademylib
import pandas as pd
orders = pd.read_csv(‘shoefly.csv’)
orders[‘shoe_source’] = orders.apply(lambda x: ‘animal’ if x.shoe_material == ‘leather’ else ‘vegan’)
print orders


_test.py script.py shoefly.csv test2.py test3.py

how do i get your website to work again?

1 Like

Is anyone on this course within the Data Science Career Path?

I feel like I’m missing a lot. It’s a thought over the last few lessons. In this lesson one of the hints mentioned “.format”, which is something I haven’t been presented with yet. When I couldn’t figure it out and did the “solution” it didn’t even have it which was weird.

I looked at the course listing in the catalog and looks like the Data Science Career Path only puts you partway through the Python 3 course. Maybe it’s ‘enough’ depending, but I’m wondering if I went in and finished that course I would have the rest of the information I need. This ‘course’ if done standalone says you should have completed all of Python 2 (where the page says to take Python 3 if you can).

So, I guess I’m wondering if anyone else if having this issue.


Yes I was I wondering the same too. There are a couple of functions that are thrown in with not much explanation what they do.


I also feel the same way. I thought that the Data Science Career Path would take us all the way through these languages but it seems there are quite a few holes in the learning path. I’ll have to take the separate SQL and Python courses to supplement my knowledge, which is very frustrating. I paid for Pro and I’m not sure I’m getting my money’s worth.


So this was my code:

orders['salutation'] = orders.last_name.apply(lambda x: 'Dear Mr. '+ x if orders.gender == 'Male' else 'Dear Ms. '+ x, axis=1

Which worked

But the solution gave 2 different codes:

orders['salutation'] = orders.apply(lambda row: 'Dear Mr. ' + row['last_name'] if row['gender'] == 'male' else 'Dear Ms. ' + row['last_name'], axis=1)

& also this code:

orders['salutation'] = orders.apply(lambda row: 'Dear Mr. {}'.format(row.last_name) if row['gender'] == 'male' else 'Dear Ms. {}'.format(row.last_name), axis=1)

My question is, is “row” equivalent to “orders”?
Why does the solution not just use orders.last_name.apply?
Is there a benefit to using orders.apply?

I am encountering the same issues. One example from this lesson are “String Methods” which were necessary for completion but I don’t recall having covered them at all before this point. This is the second time on this path that I have encountered unfamiliar concepts which are necessary for understanding the lesson but haven’t been covered. It’s very frustrating and makes me feel like I’m not grasping the basics even though there are fundamentals being left out.