Path: blob/master/section-2-data-science-and-ml-tools/pandas-exercises-solutions.ipynb
874 views
Pandas Practice (solutions)
This notebook offers a set of solutions to different tasks with pandas.
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 documnetation.
What does it show you?
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-27-0c83f8ec73bb> in <module>
1 # Try to plot the Price column using plot()
----> 2 car_sales["Price"].plot()
~/Desktop/ml-course/zero-to-mastery-ml/env/lib/python3.6/site-packages/pandas/plotting/_core.py in __call__(self, *args, **kwargs)
792 data.columns = label_name
793
--> 794 return plot_backend.plot(data, kind=kind, **kwargs)
795
796 def line(self, x=None, y=None, **kwargs):
~/Desktop/ml-course/zero-to-mastery-ml/env/lib/python3.6/site-packages/pandas/plotting/_matplotlib/__init__.py in plot(data, kind, **kwargs)
60 kwargs["ax"] = getattr(ax, "left_ax", ax)
61 plot_obj = PLOT_CLASSES[kind](data, **kwargs)
---> 62 plot_obj.generate()
63 plot_obj.draw()
64 return plot_obj.result
~/Desktop/ml-course/zero-to-mastery-ml/env/lib/python3.6/site-packages/pandas/plotting/_matplotlib/core.py in generate(self)
277 def generate(self):
278 self._args_adjust()
--> 279 self._compute_plot_data()
280 self._setup_subplots()
281 self._make_plot()
~/Desktop/ml-course/zero-to-mastery-ml/env/lib/python3.6/site-packages/pandas/plotting/_matplotlib/core.py in _compute_plot_data(self)
412 # no non-numeric frames or series allowed
413 if is_empty:
--> 414 raise TypeError("no numeric data to plot")
415
416 # GH25587: cast ExtensionArray of pandas (IntegerArray, etc.) to
TypeError: no numeric data to plot
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.