There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
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!
Hi. I am confused about when we need to use a lambda function. I became confused on Step 3 of this exercise after viewing the hint, which didn’t include the lambda function. I tried to do:
I finally requested the solution and saw that a lambda function was required. Is there any other way to solve this without a lambda function? If not, what is a good way to determine when lambda functions will be needed? I have struggled with this throughout the lessons. Thank you.
Me too. I’m also confused about when to use lambda. The hints could be better. So could the replies on these forums. I see you asked a month ago already.
One needs lambda for more complicated operations using python. I wrote this as a separate function to start with then integrated it into the single line of code. The "axis=1’ makes the code work along the row instead of the column
As I continue to do the Data Science career path, not only do the questions become more vague but also the answers to the lessons contain things that were NEVER in what the lessons taught.
If anyone could explain to me the use of the “.reset_index()” as I thought it was only used when dealing with a dataframe ( I know we are dealing with them here but I thought it was only used to change a series into a DF but maybe I don’t have my definition of “series” correct)
The following works, is simpler than lawburr’s answer, and is accepted by the code parser:
orders['month'] = orders.date.apply(lambda x : x.split('-')[0])
Unfortunately, the following is technically correct but NOT allowed by the code parser. Codecademy should use their coding skills (?) to write a code parser that does not reject valid code:
Is it just me or for the last task is it worded confusingly?
Calculate the standard deviation for the average price of orders for each month using std. Save this to std_order.
They say average price so I assumed they would want us to do something like std_orders = avg_orders.price.std()
but actually they meant find the standard deviation of the prices from the original table? Wording really threw me off.
I was confused at the same point, but it is also suggested that the purpose is to add error bars to the bar chart, so I think it is possible to guess what they request.
thanks for the answer, I have another doubt… is this correct? orders[‘month’] = orders[‘date’].apply(lambda x : x.split(’-’)[0]), what woud happens if the date column were something like: ‘actual date’
Hello, Can anyone help me with this?
Why this doesn’t work?
orders[‘month’] = orders.date.str.split(’-’)[0]
and the result of this exercise says that we have to use a lambda function
Here are two ways to work on this, I believe there are more ways, this is why it’s interesting.
**Method 1:
orders = pd.read_csv(“orders.csv”, parse_dates=[“date”])
orders[“month”] = orders.date.dt.strftime("%m")
I think the problem with orders.date.str.split(’-’)[0] is that it creates a whole series of split strings first, and then selects the item at index [0] in that series, whereas we want it to select the string with index [0] from each split string. I guess using the str.get() method forces it to apply that function to each row of the series, just like using `.apply’ with a lambda function. Thanks to @brothermarat above for giving the link to the lesson!
Can someone explain why orders.date.str.split… needs the ‘.str’? Isn’t the date already in string format?
Would orders.date.split(’-’).str.get(0) work?
Does it have something to do with the fact that we are inherently parsing (‘unpacking’) a list?
If you want to use this code, you must read in the ‘date’ column in DateTime format. pd.read_csv(‘name.csv’, parse_dates=‘colum to parse the date from’) You can call .dtype on this column and you will see these dates are strings. So your code is not correct for this particular case. I won’t work here on the platform or in “real life”.