FAQ: Data Cleaning with Pandas - String Parsing

This community-built FAQ covers the “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 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!

Why is the regex in this example ‘[$,]’? That is, why is the comma necessary? and in the solution, why is the backslash listed before %, and why is the comma necessary in the solution as well?

6 Likes

The backslash allows you to use an “escape” special characters. Both the $ and the % are special characters in python. In the exercise, the Run works with or without the backslash and the comma – at least on my browser. The comma is a convention we sometimes see employed, though it is often optional.

https://docs.python.org/3.5/library/re.html

I tried to replace ‘fract’ with ‘act’ in exam column. I got actactactactactions as a result. I used regex ^ to not match the ‘fract’ , then I got fractactactactact as a result. Is there a way to replace ‘fract’ in fractions with ‘act’ that gives us the result like ‘actions’.

students.exam = students[‘exam’].replace(’[fract,]’,‘act’,regex=True)
print(students)

I got the same result without using comma after fract. So, Is it optional to use comma?

I am using the following line of code to try and convert the column. But it says that I must return string instead of float. when did i change the type to float in this code? it seems to me like it should have been left as a string unless i am missing something.

students.score = students[‘score’].replace(’[%,]’,’’, regex=True)

I’m not sure on your second example where you use ^ unless I see that specific line of code. but for the example where you include the code this is because by using brackets you are telling python that any of these individual values is acceptable to replace with act. So it looks at the the first character in ‘fractions’ and says ‘f’ in ‘fract’ so it updates the final string to actractions. it then looks at the next character from the original string and says ‘r’ in ‘fract’ so it updates the final string to actactactions. it repeats this for the entire string and thus you end up with 5 ‘act’ strings. try something like this instead without the brackets. There are multiple right answers but this fits best into your description

students.exam = students[‘exam’].replace(’fract’,‘act’,regex=True)
print(students)

I found nothing wrong in this code.

Is there a glitch of some kind? My code was exactly the same as the solution. I even copy and paste the solution code to run step 1 and it still said “must be str, not float”.
Thu, is the code taught in this section correct?

The code:

students.score = students[‘score’].replace(’[ \ %,]’, ‘’, regex=True)

You are not alone.


but I also noticed that the data in the column was cleaned (the %'s are removed)
(note the not escaping the %…not thinking it needs escaping…though it is harmless…)

Oh shoot, no, it is obvious…I just still needed to add the to_numeric call on the result.