Jupyter notebook is giving me error for correct codes

My jupyter notebook is giving error for codes that are correct. specifically this is my error: “AttributeError: ‘NoneType’ object has no attribute ‘plot’”

Thanks in anticipation

You will have to share your code, and preferably the link to the Mission you are working on (if applicable) for others to be able to help you accordingly.

But based on just the error, you are trying to use plot with a DataFrame/Series that is currently returning a None value. You likely performed an operation before you tried to use plot which resulted in a None and hence the error. So, focus on that operation before plotting and try to see what might have gone wrong.

My jupyter notebook is giving error for codes that are correct. specifically this is my error: “AttributeError: ‘NoneType’ object has no attribute ‘plot’”

The code below give error
raw_data_count = recent_grads.shape[0]
print (raw_data_count)

Similarly, the following written codes for making plot gives error as:

recent_grads.plot(x="Sample_size", y="Median", kind = "scatter", title = "Sample_size VS Median")
recent_grads.plot(x="Sample_size", y="Unemployment_rate", kind = "scatter", title = "Sample_size VS Uemployemny")
recent_grads.plot(x="Full_time", y="Median", kind = "scatter", title = "Full_time VS Median")
recent_grads.plot(x="ShareWomen", y="Unemployment_rate", kind = "scatter", title = "Sharewoman VS Unemployment_rate")
recent_grads.plot(x="Men",y="Median", kind = "scatter", title = "Men VS Median")
recent_grads.plot(x="Women",y="Median", kind = "scatter", title = "Sample_size VS Median")

Visualizing Earnings Based On College Majors.ipynb (26.3 KB)

i have attached my jupyter notebook code here as well as the screeshot

Click here to view the jupyter notebook file in a new tab

As the error points out, you don’t have recent_grads defined. So, the following code line -

raw_data_count = recent_grads.shape[0]

throws the error. If there’s no recent_grads, the above can’t run.

The actual reason for the above is because you most likely didn’t run the Code Cells before this one that defined and initialized recent_grads. That’s why when you run the Cell with the above code, it says recent_grads is not defined.

So, all that you need to do is to make sure you run ALL Cells in the right order.

If you jump around the order, or if you worked on the Notebook last week and then you returned to it today and you try to run the 4th Cell instead of running the first 3 before it, you are bound to get such errors.

When working with Jupyter Notebooks, be mindful of which Cells you are running, when, and in what order. It’s not like a standalone python script where every line of code will run sequentially if you run the script. You have to execute/run all the cells in the right order.

The rest of your errors are related to the one above and should be fixed when you fix the above.

@the_doctor I sincerely appreciate your response to my challenges. I have implemented your suggestion but still having the same problem. i ran the code cell by cell one after the other.

Here attached are my code and error images

My jupyter notebook code file is also attachedVisualizing Earnings Based On College Majors.ipynb (27.3 KB)

Click here to view the jupyter notebook file in a new tab

As the error points out, your recent_grads is still returning a NoneType. And you can’t call any functions like count() on anything that returns a None.

So, in your code, before you run recent_grads.count() you are performing an operation where recent_grads is returning NoneType.

You need to investigate where and why that is happening. Instead of going from code cell to code cell, focus on the output of each line of code. Print out recent_grads and see what it prints at each step, think about what operation you are performing on recent_grads, what the expected output of that operation should be and why it would return a None.

For example, maybe you were trying to drop rows with NaNs and you mistakenly dropped columns with NaNs instead, and now your dataframe is empty (none type). So any subsequent operations on your dataframe won’t work if it doesn’t have any data. This is just an example to help you think through the process and not necessarily the actual problem.

This will help your debugging skills, so go through your code line-by-line and try to understand what your code is doing vs what you expect it to before moving forward.

If you still feel stuck then share your updated code and I can try to help. But I would strongly suggest you first focus on understanding and finding the problem yourself. And take your time!

1 Like

Thank you for your valuable time and support.