FAQ: Data Cleaning with Pandas - More String Parsing

This community-built FAQ covers the “More String Parsing” exercise from the lesson “Data Cleaning with Pandas”.

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

Practical Data Cleaning

FAQs on the exercise More String Parsing

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!

Step one seems to be impossible since even copying and pasting the answer from the solution does not work. I get an AttributeError and cannot continue. This issue has been plaguing me repeatedly in the pandas section as you seem to not be able to use any methods on columns

I’ve seen this error a number of times as well. This occurs whether I’m on my iMac using Firefox or Safari, or on my raspberry pi using Chromium. Sometimes refreshing the page fixes the problem. Other times, going ‘Back’ works. Other times, nothing seems to work except using the solution option. I don’t like that approach either.

Here’s my full solution:

split_grade = students['grade'].str.split('(\d+)', expand=True)
print("split_grade\n", split_grade)
students.grade = split_grade[1].replace('[\s]', '', regex=True)
print(students.dtypes)
students.grade = pd.to_numeric(students.grade)
avg_grade = students.grade.mean()
print("average grade = ", avg_grade)
print(students.dtypes)
2 Likes

I am also having the same problem and its very frustrating. I have sent a message to the bot, lets see if someone gets back. In any case I am writing to ask about a question in this excercise. When we split the grade by the digits, why is it that we need to select the second item, i.e, index 1, instead of the first one. The number is at the beginning.

students.grade = students.grade.str.split(’(\d+)’, expand = True)[1]

3 Likes
df.exercise = split_df[2].replace('[\- ]', '', regex=True)

can’t understand why [2] is the exercise column if it’s must be ‘reps’ column?

1 Like

@airens:

When we split the grade by the digits, why is it that we need to select the second item, i.e, index 1, instead of the first one. The number is at the beginning.

students.grade = students.grade.str.split(’(\d+)’, expand = True)[1]

Your question is based on what seems to be common sense, i.e., ‘The number is at the beginning of the string I want to split, so why isn’t it the first element (index 0) in the list returned by .split()?’. That common sense doesn’t work in practice, however, because of how the .split() method works. The first element actually returned from that split is an empty string: '', which if you follow the example in this exercise and create a split_df, results in an empty first column in that dataframe. You can find some more explanation and discussion about the reason for split returning an empty string at this Stack Overflow topic.

i don’t understand why on exercise 7 you can use :
“str_split.str.get(i)” to get the value of the split

and now we use " split_df[i] " to get the same value.

I thought maybe that’s because the last split returned a ‘serie’ and now in this exercise the split returns a ‘dataframe’ (because of expand=True) .
But then i tried to re-do the exercise 7 converting the serie into a dataframe with .reset_index() and i couldn’t reach it .

With the line students.grade = students.grade.str.split('(\d+)', expand = True)[1], why do we use [1]? I understand that in the resulting series, column 1 holds the grade, but when printing the series, column 0 and column 2 are empty. Why is that?