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

Hits: 61

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 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.

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners