Intro to Data Science - step 5

In Introduction to Data Science step 5, the course directions ask me to paste in some code to a Python code to generate updated results. In this case the assumption is that 15 results will be returned, with ‘urban’ and ‘rural’ columns included in the returned set. I could not get this to happen, as shown in my screen shot. What am I doing wrong? Does the ‘print’ line not always need to occur last in the code lines? This is not indicated in the lesson text.

(Also, as a comment to the lesson designer, the only reason I kind of knew where to paste the code is that I had done most of the SQL lesson series earlier. A newcomer would have no idea where to paste the code, and frankly, would be intimidated by so much code appearing so early in the lesson set. Just my two cents as a beginner.)

I can understand your confusion b/c the instructions don’t specifically say to paste the code in the script.py tab. Maybe reset this particular lesson and paste the code again to see if that resolves the issue(?)

If you’re using Jupyter Notebook or Colab, no you don’t need a print command to see the first few rows of a db. You’re using Pandas and you’d just type:

df.head(15)

(or whatever you want to see)
The default for using .head() is the first five rows of a dataframe.

If you want to see the last 5 rows of a dataframe, you can type:
df.tail(10)

You can also do:
df.info() which will tell you the data types in the df

df.shape() which will tell you how many rows/cols you have

and,

df.isnull().sum() which will tell you if you have any nulls in your df.

But that was more than you initially asked. :slight_smile:

Please include a link to the lesson in future, they can often be tricky to find.
https://www.codecademy.com/paths/data-science/tracks/dscp-getting-started-with-data-science/modules/dscp-introduction-to-data-science/lessons/data-science-process/exercises/cleaning-data

There are a few steps to complete on this page and this is not the first step. It may be that skipping ahead has caused an issue as any errors might prevent code execution. Try resetting the lesson (just this lesson) and completing the instructions in order. You should end up with three different print statements added to the code by the end of the lesson (there’s no need to remove them after each step). Comments are included in the script.py file to indicate where they should be added.

As for print no there is not strict need for print to be used at the very end of a script, but it would need to be used after the logical steps needed to create that value were performed. As a very quick example-

a = 3
print(a) # This is fine.

print(b)  # A NameError would occur here since b has not yet been assigned.
b = 10

I’ve not seen any form of interactive Python interpreter on CC yet, best to be adding output values explicitly, even if it’s just wrapping df.head(5) within print().

Much appreciated, thank you for such a comprehensive answer!

1 Like

Okay interesting, thank you!