Path: blob/master/section-2-data-science-and-ml-tools/pandas-exercises.ipynb
874 views
Pandas Practice
This notebook is dedicated to practicing different tasks with pandas. The solutions are available in a solutions notebook, however, you should always try to figure them out yourself first.
It should be noted there may be more than one different way to answer a question or complete an exercise.
Exercises are based off (and directly taken from) the quick introduction to pandas notebook.
Different tasks will be detailed by comments or text.
For further reference and resources, it's advised to check out the pandas documentation.
Note: Since you've imported ../data/car-sales.csv
as a DataFrame, we'll now refer to this DataFrame as 'the car sales DataFrame'.
What does it show you?
Why didn't it work? Can you think of a solution?
You might want to search for "how to convert a pandas string column to numbers".
And if you're still stuck, check out this Stack Overflow question and answer on turning a price column into integers.
See how you can provide the example code there to the problem here.
If you check the car sales DataFrame, you'll notice the Make column hasn't been lowered.
How could you make these changes permanent?
Try it out.
Notice how the Make column stays lowered after reassigning.
Now let's deal with missing data.
Notice the missing values are represented as NaN
in pandas DataFrames.
Let's try fill them.
We'll now start to add columns to our DataFrame.
Notice how the index numbers get moved around. The sample()
function is a great way to get random samples from your DataFrame. It's also another great way to shuffle the rows by setting frac=1
.
Notice the index numbers have been changed to have order (start from 0).
Extensions
For more exercises, check out the pandas documentation, particularly the 10-minutes to pandas section.
One great exercise would be to retype out the entire section into a Jupyter Notebook of your own.
Get hands-on with the code and see what it does.
The next place you should check out are the top questions and answers on Stack Overflow for pandas. Often, these contain some of the most useful and common pandas functions. Be sure to play around with the different filters!
Finally, always remember, the best way to learn something new to is try it. Make mistakes. Ask questions, get things wrong, take note of the things you do most often. And don't worry if you keep making the same mistake, pandas has many ways to do the same thing and is a big library. So it'll likely take a while before you get the hang of it.