Data Science Path - MODIFYING DATAFRAMES Review

Is the \ character in the following HINT correct? What does it mean since this is not a division condition? I am unable to find any reference in the lessons for “”… is it an error meant to be a pipe (|)?

(Taken from: https://www.codecademy.com/paths/data-science/tracks/dscp-data-manipulation-with-pandas/modules/dscp-hands-on-with-pandas/lessons/pandas-ii/exercises/review-ii)

QUESTION:
2. Many of our customers want to buy vegan shoes (shoes made from materials that do not come from animals). Add a new column called shoe_source , which is vegan if the materials is not leather and animal otherwise.

HINT: The following lambda function might be helpful:

mylambda = lambda x: 'animal' \   `        
if x == 'leather' else 'vegan'

Hi @mola614,

Yes, the hint is correct. The backslash (\) character is an escape character in Python, not to be confused with the forward slash (/), which represents division.

It’s referenced long before the modifying dataframes section in the Data Science path curriculum, back in the Python strings section, so it’s understandable that you may have forgotten about it.

Escape characters tell the computer to ignore the character immediately following the escape. In this example, the escape character allows the expression to be broken up onto two lines, because it escapes the newline character that normally is present when you hit Enter or Return.

However, it’s absolutely not needed in this case, so if you’d prefer to write the expression like this…

mylambda = lambda x: 'animal' if x == 'leather' else 'vegan'

…then that’s perfectly valid.

If you’d like to learn more about escape characters, there are many articles online that cover the subject.

Happy coding!

2 Likes