Introduction to Pandas

I’m working on the Introduction to Pandas section of the Analyzing data with python course- however, in doing the review of the ‘modifying dataframes’ question, I am stuck on question 3:

Our marketing department wants to send out an email to each customer. 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.

What I have trouble understanding is that the solution given is different to the hint given- I have tried to follow the hint but just don’t understand what I am doing wrong as it uses different syntax in comparison to the solution given. Would you perhaps be able to see where I am going wrong?

salutation=lambda row:

‘Dear Mr. {}’.format(row.last_name)

if row.gender== ‘male’

else ‘Dear Ms.{}’.format(row.last_name),

axis=1

orders[‘salutation’]=orders.apply(salutation)

Which axis are you meant to be using?

Hi! I’m meant to be adding this column and thus, use the information from each individual row. The code that is suggested as the correct code is this:

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

Okay, that answers that. Is it the SCT that is challenging your submission, or a console error?

Hi mtf,

Thank you for getting back to me so quickly! So I’m rather new to Codecademy and coding, so maybe you can fill me in on what SCT means?

Ok so the original code I posted, do you think that would have worked under other circumstances?

1 Like

If it is not raising any errors, and works as expected.

SCT => Submission Correctness Test

It might be a good idea to set this aside, for the moment and go through the complete Learn Python 3 and Learn Intermediate Python 3 if you haven’t already. The current course is a bit in the deep end.

Aside

Notice how the lambda is used in the Hint? It is an anonymous function written directly into the argument. That means it runs and disappears. Your code has a reference to a function that will never get called again, and is taking up memory.