September 25, 2020 - this is my output. Can you explain the syntax? I don’t understand what lambda and the notation means with x. I haven’t seen this before in the curriculum.
I tried posting a screen capture, but the uploaded screenshot just kept loading forever, so I couldn’t give you a screenshot. It never successfully uploaded.
In the code for y in range(min_year, max_year + 1): max_year is a string type value i.e made of alphanumeric characters. Also evident from the output for print(max_year) which is “September 25, 2020”.
Since it’s string type we can’t add a numeric value to it! Hence the Type Error.
Another problem here is your max_year value is wrong based on the instructions provided earlier that are on the previous screen. If you have followed that correctly, your max_year value should be 4 digit number like 1971 or 2002 etc.
You need to recheck this step.
If this one was done correctly then please share your notebook for us to have a look and help you out.
The lambda functions will be explained in future lessons. For now, understand that this code helps us to extract all the years from the data list and find out the minimum and maximum year values. That is the earliest and latest year in the Data list.
For the screenshot or file attachments (if they are taking time or hanging the screen), post your reply first. Then click on edit (it looks like a pencil) and then attach the screenshot/ file/ link etc.
I am not sure why this is happening but it’s happening for me too.
Check the instructions again from 'this task](Learn data science with Python and R projects).
It’s not wrong, but incomplete. You need to reassign the extracted year value back to row[0]. So that the first value in the list converts from “September 25, 2020” to just “2020”.
Just in case you have difficulty with the instruction and my response, this is what you are supposed to do in mission linked.
for row in data:
# fetch just the year value (2020) and save it as the first value
# in the same list
row[0] = fetch_year(row[0])
# not this
# print(row[:3])
# but this; display the first three rows of the data list
# to see the effect of the `fetch_year` method
print(data[:3])
Just copy and paste the code into a different cell, run it and observe the difference between your (original) output and the output from this one.
I see the difference. I just get the year. I understand the syntax now based on what you said here " You need to reassign the extracted year value back to row[0]. So that the first value in the list converts from “September 25, 2020” to just “2020”." Thank you!
Hi, can you please elaborate on why you need to reassign the extracted value back to row[0]? Even if you don’t reassign the value back to row[0] by running the code below, you still get the same output.
for row in data: year=fetch_year(row[0])
It gives an error message as well and I would like to understand why is that the case. This is the error message:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-42-ff478fc99572> in <module>
1 for row in data:
----> 2 year=fetch_year(row[0])
~/notebook/helper.py in fetch_year(date_string)
10
11 def fetch_year(date_string):
---> 12 return int(re.findall("\d{4}", date_string)[0])
13
14 def barplot(list_of_2_element_list):
/dataquest/system/env/python3/lib/python3.8/re.py in findall(pattern, string, flags)
237
238 Empty matches are included in the result."""
--> 239 return _compile(pattern, flags).findall(string)
240
241 def finditer(pattern, string, flags=0):
TypeError: expected string or bytes-like object