How to do a density plot using Pandas

1 min readPandasPlotStatisticsData Science
7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

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.

7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Free Newsletter

Master Data Science in Days, Not Months 🚀

Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚

Weekly simple and practical lessons
Access to ready to use code examples
Skip the math, focus on results
Learn while drinking your coffee

By subscribing, you agree to receive our newsletter. You can unsubscribe at any time.