# Pandas DataFrame plot.density() Method

Rate this post

## Preparation

Before any data manipulation can occur, three (3) new libraries will require installation.

• 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 statisticskernel 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)`

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  creates a DataFrame from a dictionary of lists and saves it to `df`.
• Line  Gets the current access (`gca()`) and saves it to `ax`.
• Line  creates a KDE chart and sets the chart title.
• Line  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.