Data Analysis with Python

What is Pandas?

Pandas is the most popular Python library for data manipulation and analysis. It provides DataFrames (like Excel sheets) and tools for cleaning, transforming, and visualizing data. Together with Matplotlib and Seaborn, it forms the backbone of data science in Python.

Setup

Install the required libraries using pip:

pip install pandas matplotlib seaborn

You can run Python scripts directly, or use Jupyter Notebook for an interactive experience.

Reading Data

Pandas can read CSV, Excel, JSON, and many other formats. The read_csv() function is the most common.

import pandas as pd

# Load the dataset
df = pd.read_csv('sales.csv')
print(df.head())   # first 5 rows
print(df.tail())   # last 5 rows

Exploring Data

Use these methods to understand your dataset quickly.

print(df.shape)        # (rows, columns)
print(df.columns)      # column names
print(df.info())       # data types, non-null counts
print(df.describe())   # summary statistics (mean, std, min, max)

Cleaning Data

Real‑world data is messy. Here are essential cleaning techniques.

# Drop rows with missing values
df.dropna(inplace=True)

# Fill missing values with the mean
df['price'].fillna(df['price'].mean(), inplace=True)

# Remove duplicate rows
df.drop_duplicates(inplace=True)

Transforming Data

Create new columns, convert data types, and apply functions.

# Create a revenue column
df['revenue'] = df['price'] * df['quantity']

# Convert to datetime
df['date'] = pd.to_datetime(df['date'])

# Extract year and month
df['year'] = df['date'].dt.year
df['month'] = df['date'].dt.month

Grouping & Aggregating

Group data by one or more columns and perform aggregations like sum, mean, count.

# Total revenue per product
revenue_by_product = df.groupby('product')['revenue'].sum()

# Top 5 products by revenue
top5 = revenue_by_product.nlargest(5)
print(top5)

Visualizing

Matplotlib and Seaborn create publication‑quality charts. Here's how to plot the top products.

import matplotlib.pyplot as plt
import seaborn as sns

# Set style
sns.set_style('whitegrid')

# Bar chart
top5.plot(kind='bar', color='steelblue')
plt.title('Top 5 Products by Revenue')
plt.xlabel('Product')
plt.ylabel('Revenue ($)')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

Interactive Python Playground

Test your Python data analysis scripts in the embedded editor below. For a full development environment, use Replit or Google Colab.