Preparation
Before any data manipulation can occur, three (3) new libraries will require installation.
- The Pandas library enables access to/from a DataFrame.
- The Matplotlib library displays a visual graph of a plotted dataset.
- The Scipy library allows users to manipulate and visualize the data.
To install these libraries, navigate to an IDE terminal. At the command prompt ($
), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($
). Your terminal prompt may be different.
$ pip install pandas
Hit the <Enter>
key on the keyboard to start the installation process.
$ pip install matplotlib
Hit the <Enter>
key on the keyboard to start the installation process.
$ pip install scipy
Hit the <Enter>
key on the keyboard to start the installation process.
If the installations were successful, a message displays in the terminal indicating the same.
Feel free to view the PyCharm installation guide for the required libraries.
Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.
import pandas as pd import matplotlib.pyplot as plt import scipy
DataFrame Plot Density
The dataframe.plot.density()
method generates Kernel Density Estimate (KDE) plots using Gaussian kernels.
Direct Quote from Wikipedia:
In statistics, kernel density estimation (KDE) is a non-parametric way to estimate the probability density function of a random variable. Kernel density estimation is a fundamental data smoothing problem where inferences about the population are made, based on a finite data sample.
from Wikipedia
The syntax for this method is as follows:
DataFrame.plot.density(bw_method=None, ind=None, **kwargs)
Parameter | Description |
---|---|
bw_method | This parameter calculates the bandwidth. This parameter can be: 'scott' , 'silverman' , scalar , or callable. Click here for details. |
ind | This parameter is the evaluation point for a PDF. If empty, 100 equally spaced points are assumed. |
**kwargs | The keyword arguments for this method are outlined in the plot method. |
For this example, a KDE chart plots the number of students who attended Grades 10 and 11 at Simms High School over the previous ten (10) years.
df = pd.DataFrame({ 'Grade-10': [12, 11, 13, 14, 17, 11, 18, 29, 47, 76], 'Grade-11': [11, 16, 15, 28, 35, 36, 61, 68, 59, 67]}) ax = plt.gca() df.plot.kde(title="KDE - Students - Previous 10 Years", ax=ax) plot.show()
- Line [1] creates a DataFrame from a dictionary of lists and saves it to
df
. - Line [2] Gets the current access (
gca()
) and saves it toax
. - Line [3] creates a KDE chart and sets the chart title.
- Line [4] outputs the KDE chart on-screen.
Output

π‘Β Note: Another way to create this chart is with the plot()
method and the kind
parameter set to the 'kde'
option.
More Pandas DataFrame Methods
Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:
Also, check out the full cheat sheet overview of all Pandas DataFrame methods.