 # Multiple Density Plots with Pandas in Python

## Multiple Density Plots with Pandas in Python

Multiple density plots are a great way of comparing the distribution of multiple groups in your data.  We can make multiple density plots using pandas plot.density() function. However, we need to convert data in a wide format if we are using the density function. Wide data represents different groups in different columns. We convert data in a wide format using Pandas pivot() function.

Let’s create the simple data-frame and then reshape it into a wide-format:

Example 1:

Here we are using this data set.

Step 1: Creating dataframe from data set.

 `import` `pandas as pd` ` ` `# creating a dataframe` `df ``=` `pd.read_csv(r``"gapminder1.csv"``)` `df.head()`

Output: dataset

Step 2: Let’s group data according to countries in different columns so that we can apply the density() function to plot multiple density plots.

 `# converting data into wide-format` `data_wide ``=` `df.pivot(columns``=``'continent'``,` `                     ``values``=``'lifeExp'``)` `data_wide.head()`

Output: Step 3: Now let’s plot multiple density plot using plot.density()

 `import` `matplotlib.pyplot as plt` ` ` `# calling density() to make` `# multiple density plot ` `data_wide.plot.density(figsize ``=` `(``7``, ``7``),` `                       ``linewidth ``=` `4``)` ` ` `plt.xlabel(``"life_Exp"``)`

Output : Multiple density plots

Example 2: We can also call plot.kde() function on dataframe to make multiple density plots with Pandas.

Here we are using the tips dataset for this example, You can find it here.

Step 1: Creating dataframe from data set.

 `import` `pandas as pd` ` ` `# creating a dataframe` `df ``=` `pd.read_csv(r``"tips.csv"``)` `df.head()`

Output: tips_df

Step 2: Now apply pivot() function to have dataframe in the wide-format then apply kde() to have multiple density plot.

 `# Converting to wide dataframe` `data_wide ``=` `df.pivot(columns ``=` `'day'``,` `                     ``values ``=` `'total_bill'``)` ` ` `# plotting multiple density plot` `data_wide.plot.kde(figsize ``=` `(``8``, ``6``),` `                   ``linewidth ``=` `4``)`

Output: tips multiple D.P

Last Updated on October 24, 2021 by admin

## Drop rows from Pandas dataframe based on certain condition applied on a columnDrop rows from Pandas dataframe based on certain condition applied on a column

Pandas provides a rich collection of functions to perform data analysis in Python. While performing data analysis, quite often we require to filter the data to remove unnecessary rows or columns. In this post we are going to discuss several

## How to read all CSV files in a folder in Pandas?How to read all CSV files in a folder in Pandas?

How to read all CSV files in a folder in Pandas? In this article, we will see how to read all CSV files in a folder into single Pandas dataframe. The task can be performed by first finding all CSV

## How to compare the elements of the two Pandas Series?How to compare the elements of the two Pandas Series?

How to compare the elements of the two Pandas Series? Sometimes we need to compare pandas series to perform some comparative analysis. It is possible to compare two pandas Series with help of Relational operators, we can easily compare the corresponding elements

## Check if dataframe contains infinity in Python – PandasCheck if dataframe contains infinity in Python – Pandas

When working with data in Python using Pandas, it is important to ensure that the data is clean and contains no unexpected values that could lead to errors in analysis or modeling. One such unexpected value is infinity. In this

## Count number of columns of a Pandas DataFrameCount number of columns of a Pandas DataFrame

Count number of columns of a Pandas DataFrame Let’s discuss how to count the number of columns of a Pandas DataFrame. Lets first make a dataframe. Example: # Import Required Libraries import pandas as pd import numpy as np   # Create

## Python | Pandas Series.replace()Python | Pandas Series.replace()

Python | Pandas Series.replace() Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for

## Python | Pandas.melt()Python | Pandas.melt()

Python Pandas.melt() To make analysis of data in table easier, we can reshape the data into a more computer-friendly form using Pandas in Python. Pandas.melt() is one of the function to do so.. Pandas.melt() unpivots a DataFrame from wide format

## How to get column and row names in DataFrame?How to get column and row names in DataFrame?

Get column and row names in DataFrame   While analyzing the real datasets which are often very huge in size, we might need to get the rows or index names and columns name in order to perform some certain operations.