Code error in one of the exercises

I am getting this error:

Copy
Next, let’s use the same regular expression from the last screen to extract all the years from the Special Notes column, except this time, we’ll use a named capturing group. Using a named capturing group means that we can refer to the group by the specified name instead of just a number. We can use the following syntax to add a name: (?P<Column_Name>…).

Below, we name the capturing group Years:

pattern = r"(?P[1-2][0-9]{3})"
merged[‘SpecialNotes’].str.extractall(pattern)

Copy
Below are the first five rows of the output:

Extractall
Let’s look at the IESurvey column next. This column has years in two different formats:

Integrated household survey (IHS), 2012
Integrated household survey (IHS), 2010/11

Copy
Let’s test the code above on this column to see if we can extract all of the years from the IESurvey column.

Instructions

We’ve already set the Country column as the index and saved the regular expression used to extract years in the pattern variable.

Use the Series.str.extractall() method to extract all of the years in the IESurvey. Assign the result to years.
Use the Series.value_counts() method to create a list of the unique years, along with the count. Assign the result to value_counts. Print value_counts.

Get Help
script.py
World_Happiness_2015.csv
World_dev.csv
.
1

The Series.str.extract() method will only extract the match of the pattern.

2
# If we wanted to extract all of the matches, we can use the Series.str.extractall() method
3
merged = merged.set_index(“Country”)# Results made easier to read by using the df.set_index() to set the Country column as the index
4

Next, we use the same regular exptression to extract all the years from the Special Notes column except this time, we’ll use a named capturing group

5
# Using a named capturing group means that we can refer to the group by the specified name instead of just a number
6
# We can use the following syntax to add a name: (?P<column_name>…)
7

Below we name the capturing group Years

8
pattern = r"(?P[1-2][0-9]{3})"
9
merged[‘SpecialNotes’].str.extractall(pattern)
10
merged.info()
Output

KeyError Traceback (most recent call last)
in
8 # The Series.str.extract() method will only extract the match of the pattern.
9 # If we wanted to extract all of the matches, we can use the Series.str.extractall() method
—> 10 merged = merged.set_index(“Country”)# Results made easier to read by using the df.set_index() to set the Country column as the index
11 # Next, we use the same regular exptression to extract all the years from the Special Notes column except this time, we’ll use a named capturing group
12 # Using a named capturing group means that we can refer to the group by the specified name instead of just a number

/dataquest/system/env/python3/lib/python3.8/site-packages/pandas/core/frame.py in set_index(self, keys, drop, append, inplace, verify_integrity)
4301
4302 if missing:
→ 4303 raise KeyError(f"None of {missing} are in the columns")
4304
4305 if inplace:

KeyError: “None of [‘Country’] are in the columns”
I followed everything that is on the learning path and my code is as follows:

The Series.str.extract() method will only extract the match of the pattern.

# If we wanted to extract all of the matches, we can use the Series.str.extractall() method

merged = merged.set_index(“Country”)# Results made easier to read by using the df.set_index() to set the Country column as the index

Next, we use the same regular exptression to extract all the years from the Special Notes column except this time, we’ll use a named capturing group

# Using a named capturing group means that we can refer to the group by the specified name instead of just a number 
    # We can use the following syntax to add a name: (?P<column_name>...)

Below we name the capturing group Years

pattern = r"(?P[1-2][0-9]{3})"
merged[‘SpecialNotes’].str.extractall(pattern)
merged.info()
Thank you
-Salem

Hi @salemabdulkerim, what you posted is kind of confusing, can you add the link to the exercise?

@OlutokiJohn,

Yep. Here is the link:

I am still getting the same error

I don’t get your question still, the question is a continuation of what you’ve been doing before in the previous slide i.e you’re supposed to apply the pandas’ regex function to extract all of the years in the IESurvey. and Assign the result to years. then you use the value_counts method to list the unique years along the count. then you print it.

in place of specialNotes input IESurvey and assign it to years then

So I was doing the lesson. I followed the instructions from the learning section and I got this error:

KeyError Traceback (most recent call last)
in
8 # The Series.str.extract() method will only extract the match of the pattern.
9 # If we wanted to extract all of the matches, we can use the Series.str.extractall() method
—> 10 merged = merged.set_index(‘Country’)# Results made easier to read by using the df.set_index() to set the Country column as the index
11 # Next, we use the same regular expression to extract all the years from the Special Notes column except this time, we’ll use a named capturing group
12 # Using a named capturing group means that we can refer to the group by the specified name instead of just a number

/dataquest/system/env/python3/lib/python3.8/site-packages/pandas/core/frame.py in set_index(self, keys, drop, append, inplace, verify_integrity)
4301
4302 if missing:
→ 4303 raise KeyError(f"None of {missing} are in the columns")
4304
4305 if inplace:

KeyError: “None of [‘Country’] are in the columns”
I did not do the exercise yet. I am trying to figure out why I am getting this error.