How data manipulate in Seaborn done to create the plots?

In Seaborn, data manipulation is done using pandas, which is a powerful data manipulation library in Python. Seaborn is built on top of pandas and integrates seamlessly with it. Pandas provides data structures and functions for filtering, grouping, aggregating, and transforming data, which can be used with Seaborn to create visualizations.

By combining pandas data manipulation with Seaborn plotting functions, we can easily manipulate and visualize data in a concise and efficient manner. This allows us to explore and communicate insights effectively from our datasets.

Here's a step-by-step guide on how data manipulation is done using pandas in Seaborn to create plots ?

Import the Necessary Libraries

First, we need to import pandas and Seaborn libraries ?

import seaborn as sns
import pandas as pd

Load or Create Your Dataset

Next, we can load or create our dataset using pandas. Here we'll create a sample dataset using the DataFrame() function ?

import seaborn as sns
import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'Salary': [50000, 60000, 70000]}
df = pd.DataFrame(data)
print(df)
      Name  Age  Salary
0    Alice   25   50000
1      Bob   30   60000
2  Charlie   35   70000

Data Manipulation Operations

Once we have our dataset in a pandas DataFrame, we can use various data manipulation techniques to prepare the data for plotting ?

Filtering Data

Filtering selects a subset of rows based on certain conditions. For example, filtering rows where age is greater than 28 ?

import seaborn as sns
import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'Salary': [50000, 60000, 70000]}
df = pd.DataFrame(data)

filtered_df = df[df['Age'] > 28]
print(filtered_df)
      Name  Age  Salary
1      Bob   30   60000
2  Charlie   35   70000

Grouping and Aggregating

Grouping data by variables and calculating summary statistics. Here we group by name and calculate mean salary ?

import seaborn as sns
import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie', 'Alice', 'Bob'],
        'Department': ['IT', 'Finance', 'IT', 'Finance', 'IT'],
        'Salary': [50000, 60000, 70000, 55000, 65000]}
df = pd.DataFrame(data)

grouped_df = df.groupby('Name')['Salary'].mean()
print(grouped_df)
Name
Alice      52500.0
Bob        62500.0
Charlie    70000.0
Name: Salary, dtype: float64

Data Transformation

Data transformation applies functions to modify data or create new columns based on existing ones ?

import seaborn as sns
import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'Salary': [50000, 60000, 70000]}
df = pd.DataFrame(data)

# Create a new column with salary in thousands
df['Salary_K'] = df['Salary'] / 1000
print(df)
      Name  Age  Salary  Salary_K
0    Alice   25   50000      50.0
1      Bob   30   60000      60.0
2  Charlie   35   70000      70.0

Data Reshaping

Reshaping restructures data to different formats using techniques like pivoting or melting ?

import seaborn as sns
import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Q1_Sales': [100, 120, 150],
        'Q2_Sales': [110, 130, 160]}
df = pd.DataFrame(data)

# Melt to convert from wide to long format
melted_df = df.melt(id_vars=['Name'], var_name='Quarter', value_name='Sales')
print(melted_df)
      Name   Quarter  Sales
0    Alice  Q1_Sales    100
1      Bob  Q1_Sales    120
2  Charlie  Q1_Sales    150
3    Alice  Q2_Sales    110
4      Bob  Q2_Sales    130
5  Charlie  Q2_Sales    160

Creating Plots with Seaborn

Once the data is prepared, we can use Seaborn's plotting functions to create visualizations. Here's an example creating a bar plot ?

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

data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'Salary': [50000, 60000, 70000]}
df = pd.DataFrame(data)

# Create a bar plot
sns.barplot(x='Name', y='Salary', data=df)
plt.title('Salary by Name')
plt.ylabel('Salary ($)')
plt.show()

Common Seaborn Plot Types

Plot Type Function Best For
Scatter Plot scatterplot() Relationship between two continuous variables
Line Plot lineplot() Trends over time or continuous data
Bar Plot barplot() Comparing categories
Box Plot boxplot() Distribution and outliers
Histogram histplot() Distribution of single variable

Conclusion

Seaborn leverages pandas for data manipulation, providing a powerful combination for data visualization. By filtering, grouping, transforming, and reshaping data with pandas, we can prepare datasets for effective visualization using Seaborn's plotting functions.

Updated on: 2026-03-27T10:52:10+05:30

299 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements