How to do a density plot using Pandas

1 min

Density plots are extremely useful when you want to do some data exploration.

With density plots you can determine what kind of data you are looking at.

Together with the histogram, they are building blocks of data exploration.

Here is how to do a density plot using the Pandas library.

# How to do a density plot using Pandas
# We import the pandas library
import pandas as pd

# We create our sample dataframe
# The col1 variable has a mean of 10 and a std of 5
# The col2 variable has a mean of 20 and a std of 10
df = pd.DataFrame({"col1": [6, 12, 12, 4, 21, 12, 15, 11, 16, 8],
                   "col2": [20, 26, 26, 1, 11, 18, 20, 12, 23, 19]})

df.plot(kind='density')

As we can see we are using the DataFrame.plot() method that will plot an density plot as specified with the kind="density" parameter.

Here is the result.