How to do a density plot using Pandas
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.
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.
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.
Related Articles
Continue your learning journey with these related topics
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. 📚