This community-built FAQ covers the “Applying a Lambda to a Column” exercise from the lesson “Modifying DataFrames”.
Paths and Courses
This exercise can be found in the following Codecademy content:
Data Analysis with Pandas
FAQs on the exercise Applying a Lambda to a Column
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply (
) below!
Agree with a comment or answer? 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 can’t we do it in one go as well as in the example? I would have assumed this:
df[‘Last Name’]= df.name.apply(lambda x: x.split()[-1])
should work just as well but it doesn’t.
Is it only because we want the lambda function to have a certain name?
my table doesn’t get altered if i run this code, though
edit: nevermind, it works and my table does get altered. only after i’ve looked at the solution, though, because the exercise was blocking it before i think and giving me strange error messages.
5 Likes
I had the same problem. After I looked at the solution, it seemed one has to break it down into two steps
Why does the lambda used in the solution not require a variable passed to it as lambda functions required in previous examples?
The solution just uses the lambda “get_last_name” as follows:
df[‘last_name’] = df.name.apply(get_last_name)
I had assumed the lambda would need something passed to it like what I tried below:
#df[‘last_name’] = df.apply(get_last_name(df.name))
2 Likes
I was thinking that as well.
you are implicitly passing the name as df.name.apply(), it’s just in the reverse order you’re using to seeing a function(arg) call, as it is arg(function)
I tried a similar code
df[‘last name’] = df[‘name’].apply(lambda x: x.split(’ ')[-1])
It added the column named “last name” to my table with the only the last names. But, the exercise does not take the answer as valid and I have not clue why.
Also, I tried creating a separate table that contains the last names. with the code below
get_last_name = df[‘name’].apply(lambda x: x.split(’ ')[-1])
print(get_last_name)
I tried to view the answer and it gets stuck getting to the page where it shows you the comparison between my answer and solution…
Any clue about where is my mistake?
Hi I get an error in this exercises when the output in the console was what they ask me for. I would like to know if it was wrong or you can do both ways?
My code was:
df['get_last_name'] = df.name.apply(lambda x: x.split()[-1])
and the rigth code was:
get_last_name = lambda x: x.split()[-1]
df['last_name'] = df.name.apply(get_last_name)
I get the same Error than you , i think. Because the exercise was in two steps and we do it in one.
I hope my answer help you, and keep going; ** We are doing good!!**
Why is it not possible to do
df.last_name = df.name.apply(get_last_name)
Why do you have to use square bracket syntax for the column name:
df["last_name"] = df.name.apply(get_last_name)
1 Like
Is this because of the apply() function? Does it make the col the argument for any function passed into it?
This wasn’t really explained in the apply() section.
Because a column of data in a Pandas df is a series, so, you use brackets.
You could try/see:
print(type(df['col_name']))
>><class 'pandas.core.series.Series'>
See:
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.html
Just to be clear, the single code method with the lambda built into the apply does work in this exercise. The issue in both of these cases is because they did not follow the instruction properly and having the correct column.
The exercise explicitly asks
Use the lambda function get_last_name
to create a new column last_name
with only the employees’ last name.
It may not be obvious, but it is important to distinguish between your outputs and your product. Even if your output looks right and you are getting the right display, in real work situation often times there are other codes and products downstream from what you have created. If you are asked to create columns and codes with specific names, it’s likely because someone downstream is calling those names and applying it using the dataframe you have created. If your naming convention is not following what others are doing exactly, you will cause error in their work and creating more issues for the team.
Paying attention to detail pays dividends in this line of work.
What’s your question? What code have you written?
Please post formatted code and a link to the lesson your question is regarding.
i didn’t start on the section im on but i was wondering if i can have some help as the view solution button isnt working
This
doesn’t belong here. This is a Python FAQ. Please delete your JS post.
Delete this post too as it belongs in the JS bug issue topic that you created. I also responded to your query that you can report any course bugs within the lesson itself.
No, I cannot. Again, this is a Python FAQ (read the topic above).
Please familiarize yourself with the rules of this community & posting topics as well.
You created this:
yea i created that but i didnt see a java script thing on here so u can just get someone to help