How to use seaborn to visualise a Pandas dataframe?

How to use seaborn to visualise a Pandas dataframe?

How to use seaborn to visualise a Pandas dataframe?

This recipe helps you use seaborn to visualise a Pandas dataframe


Recipe Objective

Have you ever feel a need to visualize the data in various form. Visualizing the data give us a better idea how our dataset is distributed.

So this is the recipe on how we use seaborn to visualise a Pandas dataframe.

Step 1 - Import the library

import pandas as pd import random import matplotlib.pyplot as plt import seaborn as sns

We have imported various modules like pandas, random, matplotlib and seaborn which will be need for the dataset.

Step 2 - Setting up the Data

We have created a empty dataset and then by using random function we have created set of random data and stored in X and Y. We have used print function to print the dataset. df = pd.DataFrame() df['x'] = random.sample(range(1, 50), 25) df['y'] = random.sample(range(1, 100), 25) print(); print(df.head()) print(); print(df.tail())

Step 3 - Ploting different Plots

So we will be ploting different plots by using seaborn.

  • First we are ploting Scatterplot by passing the required parameters
  • sns.lmplot('x', 'y', data=df, fit_reg=False)
  • Now we are ploting a regression line which fits the data
  • sns.lmplot('x', 'y', data=df, fit_reg=True)
  • Now we are ploting a density plot for the data
  • sns.kdeplot(df.y); sns.kdeplot(df.y, df.x); sns.distplot(df.x);
  • Now we are ploting a histogram for the data
  • plt.hist(df.x, alpha=.3) sns.rugplot(df.x)
  • Now we are ploting a Boxplot for the data
  • sns.boxplot([df.y, df.x])
  • Now we are ploting a Violin Plot for the data
  • sns.violinplot([df.y, df.x])
  • Now we are ploting a Heatmap for the data
  • sns.heatmap([df.y, df.x], annot=False, fmt="d")
  • Finally we are ploting a clustermap for the data
  • sns.clustermap(df)
So the output comes as:

    x   y
0  15  22
1  36  61
2  39  71
3   3  46
4  38  85

     x   y
20   6  49
21  19  20
22   9  73
23  33  79
24  40  59

Relevant Projects

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.