FAQ: Project: Board Slides for FoodWheel - Orders Over Time

This community-built FAQ covers the “Orders Over Time” exercise from the lesson “Project: Board Slides for FoodWheel”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Science

FAQs on the exercise Orders Over Time

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 (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 (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!

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:

orders[‘month’] = orders.date.split(’-’)[0]

When that didn’t work, I tried variations of:

orders[‘month’] = orders.apply(orders.date.split(’-’)[0])

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.

4 Likes

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.

This is what worked for me

orders['month'] = orders.apply(lambda x: x['date'].split('-')[0],axis=1)

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

5 Likes

In this lesson you can find answer https://www.codecademy.com/courses/practical-data-cleaning/lessons/pandas-data-cleaning/exercises/splitting-char?action=resume_content_item

In the Data Science carrer path, the lesson you are refering to is latter than when the question is asked in the Food Wheel project

2 Likes

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)

6 Likes

@tlspurlin, I started reporting content bugs on directly in the help section of the lesson content. Maybe it will help Codecademy pinpoint the issue ?

1 Like

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:

orders['month'] = pd.DatetimeIndex(orders['date']).month
6 Likes

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.

5 Likes

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

1 Like

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")

**Method 2:
orders = pd.read_csv(“orders.csv”)
orders.date.apply(lambda i: i.split(’-’)[0])

1 Like

This was the solution I came up with too, but as you mention, it is not accepted. I’ll try out the lambda function I guess…

I too thought this was confusing. My first thought was to import numpy. They did not want us to use numpy, I guess.

That’s exactly what I tried first too. I eventually found that this is how to get what we both wanted:

orders['month'] = orders.date.str.split('-').str.get(0)

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!

Thanks for all the above solutions. I tried the DatetimeIndex without success and failed with split(’-’)[0],
so I wrote this monstrosity.

dates = []
for date in orders.date:
  dates.append(date)
months = []
for date in dates:
  months.append(date.split('-')[0])
orders['month'] = months

I’m wondering for experienced coders, how jive does this look? It works, but it’s 6 extra lines of code.

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”.