FAQ: Modifying DataFrames - Review


#1

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!


#2

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!


#3

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.
try

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

#4

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)
image.

Can someone elaborate why it is like this?


#5

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?


#6

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.


#7

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.


#8

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.