How to use SEABORN package to visualise a Pandas DataFrame in Python

How to use SEABORN package to visualise a Pandas DataFrame in Python

Seaborn is a popular data visualization library for Python that can be used to create beautiful and informative visualizations of data. One way to use Seaborn is to visualize a Pandas DataFrame.

First, you need to import the Pandas library and create a DataFrame. For example, you can create a DataFrame with some sample data.

import pandas as pd

data = {'product': ['Apple', 'Banana', 'Cherry', 'Date', 'Eggplant'],

'price': [1.2, 2.3, 2.5, 1.7, 2.0],

'quantity': [3, 5, 2, 4, 8]}

df = pd.DataFrame(data)

You will also need to import the Seaborn library,

import seaborn as sns

Next, you can use Seaborn’s various visualization functions to create different types of plots using the DataFrame.

For example, you can use the boxplot function to create a box plot of the ‘price’ column.

sns.boxplot(x = 'price', data = df)

You can also use the barplot function to create a bar plot of the ‘quantity’ column.

sns.barplot(x = 'product', y = 'quantity', data = df)

 

In this Learn through Codes example, you will learn: How to use SEABORN package to visualise a Pandas DataFrame in Python.

The code provided below is a Python script that demonstrates how to use the Seaborn library to visualize a Pandas dataframe. The Seaborn library is built on top of Matplotlib, and it makes it easy to create beautiful and informative statistical graphics.

The script starts by importing several libraries, including Pandas, which is used to work with dataframes, Matplotlib, which is used to create plots, and Seaborn, which is used to create statistical graphics. Then it creates an empty dataframe, which is a table-like data structure where data can be organized and analyzed.

The script then uses the random library to generate some random data and store them in the dataframe as columns ‘x’ and ‘y’.

Next, the script uses the Seaborn library to create several types of plots to visualize the data in the dataframe.

First, it creates a scatter plot of the data by calling sns.lmplot() function which accepts the name of the x and y axis, dataframe and set fit_reg=False (to turn off the regression line in scatterplot)

Then it creates a scatter plot again with a regression line, by calling sns.lmplot() function but with fit_reg=True, this would show the relation of the two column x and y

Next, it creates density plots by calling sns.kdeplot() function, which plots a kernel density estimate of the input data. The function accepts a single column of the dataframe, or it can be called with two columns to create a 2D density plot.

Then it plots Histogram and Rugplot by calling plt.hist() and sns.rugplot().

Also, it creates boxplot, violinplot, heatmap and clustermap of the data by calling sns.boxplot(), sns.violinplot(), sns.heatmap() and sns.clustermap().

Finally, the script displays all the plots using the plt.show() function.

Overall, this script demonstrates how to use Seaborn to create different types of plots to visualize a Pandas dataframe and it’s useful for a beginner who wants to learn how to visualize data using python and seaborn.



Find more … …

Data Science Project – Data Visualization with Seaborn in Python

Data Wrangling in Python – How to Use Seaborn To Visualize A pandas Dataframe

Data Viz in Python – Creating A Time Series Plot With Seaborn And pandas

Python Scatter Plot