Visualization & EDA (Exploratory Data Analysis)
Python tutorial · PySpark.in
Visualization & EDA (Exploratory Data Analysis)
Covering Pandas plotting, Histogram, Bar, Line, Box plots, and Pandas + Matplotlib / Seaborn
What is EDA?
EDA (Exploratory Data Analysis) means:
Understanding data using charts, plots, and statistics before applying ML models.
Purpose:
- Find patterns
- Detect outliers
- Check distributions
- Compare categories
- Decide preprocessing steps
Sample Dataset
```
import pandas as pd
df = pd.DataFrame({
'City': ['Delhi', 'Mumbai', 'Chennai', 'Delhi', 'Mumbai'],
'Sales': [100, 150, 120, 130, 160],
'Year': [2021, 2021, 2021, 2022, 2022]
})
print(df)
```
Plotting with Pandas
Pandas has built-in plotting using Matplotlib internally.
Basic Syntax
```
hide
df.plot()
```
Example
```
df['Sales'].plot()
```
Creates a line plot by default
Why use Pandas plotting?
✔ Quick visualization
✔ Less code
✔ Good for EDA
Histogram
What is Histogram?
- Shows distribution of numerical data
- Divides data into bins
Why use it?
✔ Check data spread
✔ Detect skewness
✔ Identify outliers
```
df['Sales'].plot(kind='hist', bins=5)
```
Output Meaning
- X-axis → Sales values
- Y-axis → Frequency
- Tall bar = common value range
Used before normalization & scaling
Bar Plot 📉
What is Bar Plot?
- Compares categorical data
- Height of bar = value
Example
```
df.groupby('City')['Sales'].mean().plot(kind='bar')
```
Output Meaning
- Each bar = City
- Height = average sales
Best for:
- Category comparison
- Business reports
- EDA summaries
Line Plot
What is Line Plot?
- Shows trend over time
- Continuous data
Example
```
df.groupby('Year')['Sales'].sum().plot(kind='line')
```
Output Meaning
- X-axis → Year
- Y-axis → Total Sales
- Shows growth or decline
Used for:
- Time series
- Stock prices
- Sales trends
Box Plot 📦
What is Box Plot?
- Shows distribution + outliers
- Based on quartiles
Components
- Median
- Q1, Q3
- Whiskers
- Outliers
```
df['Sales'].plot(kind='box')
```
Output Meaning
- Dots outside box = outliers
- Helps in anomaly detection
Very important for data cleaning
Pandas + Matplotlib / Seaborn
Pandas + Matplotlib
```
import matplotlib.pyplot as plt
df['Sales'].plot(kind='hist')
plt.title("Sales Distribution")
plt.xlabel("Sales")
plt.ylabel("Frequency")
plt.show()
```
✔ More control
✔ Labels, titles, colors
Pandas + Seaborn (Advanced & Beautiful)
```
import seaborn as sns
sns.boxplot(x='City', y='Sales', data=df)
```
✔ Cleaner plots
✔ Statistical visuals
✔ Widely used in ML projects
More Python tutorials
- What is Python and Why is it used for Data Science and Data Engineering?
- How Does Python Work in the Backend? Internal Working of Python
- Top 30 Python Interview Questions for Data Science
- 3-Month Python Roadmap to Excel in Data Science and Machine Learning
- Python Data Types Explained – A Beginner’s Guide
- test
All tutorials · Try the free PySpark compiler · Practice challenges