FAQ: Modifying DataFrames - Reviewing Lambda Function: If Statements


#1

This community-built FAQ covers the “Reviewing Lambda Function: If Statements” 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 Reviewing Lambda Function: If Statements

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

Got a SyntaxError on:

mylambda = lambda x: “Welcome to BattleCity!”/
if x >= 13/
else “You must be over 13”

(Have tried with print/return and all sort of ([parenthesis])

Whats wrong?


#3

Hi, I suggest trying without the line continuations if you haven’t already (they’re also shown as back slashes in the instructions).


#4

Also note that if you use a backslash in this case you would still get a syntax error as you will need a space before or after it because the backslash enables the line of code to work on multiple lines. e.g.

lambda x: [OUTCOME IF TRUE] if [CONDITIONAL] else [OUTCOME IF FALSE]

You can write the whole expression in one line however this can get messy and can be harder to understand when the filled statements get larger in size. this is why it might be easier to break it down into smaller portions which is given in the example below.

lambda x: [OUTCOME IF TRUE] \     #has space before backslash
    if [CONDITIONAL] \            #will now space out the word 'if' from last line of code
    else [OUTCOME IF FALSE]