Python for Data Analysis
Beginner Scripts & Cheat Sheet
Everything you need to start analyzing data with Python
• Setting up your Python environment
• Pandas basics - loading, viewing, cleaning data
• Data manipulation - filtering, grouping, merging
• Basic statistics and analysis
• Data visualization with Matplotlib & Seaborn
• Real-world mini projects to practice
1. Setting Up Your Environment
Before we start, let's install the essential libraries. Open your terminal or command prompt and run:
pip install pandas numpy matplotlib seaborn openpyxl
■ Pro Tip: Use Jupyter Notebook or Google Colab for interactive coding. It's way easier to see your
results immediately.
Basic imports you'll use in every project:
import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns
# Optional: Make plots look nicer
[Link]('seaborn-v0_8-whitegrid')
sns.set_palette("husl")
2. Loading Data
Pandas can read almost any data format. Here are the most common ones:
CSV Files (most common)
# Basic loading
df = pd.read_csv('sales_data.csv')
# With specific options
df = pd.read_csv('sales_data.csv',
encoding='utf-8', # Handle special characters
sep=',', # Separator (use ';' for some EU files)
header=0, # Row number for column names
index_col='id', # Set a column as index
parse_dates=['date']) # Auto-convert date columns
Excel Files
# Read Excel file
df = pd.read_excel('[Link]')
# Read specific sheet
df = pd.read_excel('[Link]', sheet_name='Sales')
# Read multiple sheets
all_sheets = pd.read_excel('[Link]', sheet_name=None) # Returns dict
Other formats
# JSON
df = pd.read_json('[Link]')
# SQL Database
import sqlite3
conn = [Link]('[Link]')
df = pd.read_sql('SELECT * FROM sales', conn)
# From URL
df = pd.read_csv('[Link]
3. Exploring Your Data (First Thing You Should Do)
When you load any dataset, ALWAYS start with these commands:
# Shape - how many rows and columns?
[Link] # Returns (rows, columns)
# First few rows
[Link]() # First 5 rows
[Link](10) # First 10 rows
# Last few rows
[Link]()
# Column names
[Link]
# Data types of each column
[Link]
# Quick statistics
[Link]() # Count, mean, std, min, max for numeric columns
# Info about the dataframe
[Link]() # Shows datatypes, non-null counts, memory usage
# Check for missing values
[Link]().sum() # Count of nulls per column
# Unique values in a column
df['category'].unique()
df['category'].nunique() # Count of unique values
# Value counts (frequency)
df['category'].value_counts()
■ Always run [Link]() and [Link]() first. They tell you 80% of what you need to know about your
data.
4. Selecting & Filtering Data
Selecting Columns
# Single column (returns Series)
df['name']
# Multiple columns (returns DataFrame)
df[['name', 'age', 'salary']]
# All columns except some
[Link](columns=['unwanted_col'])
Selecting Rows
# By index position
[Link][0] # First row
[Link][0:5] # First 5 rows
[Link][-1] # Last row
# By label/index
[Link][0] # Row with index 0
[Link][0:5] # Rows with index 0 to 5 (inclusive)
# Specific rows and columns
[Link][0:5, 0:3] # First 5 rows, first 3 columns
[Link][0:5, ['name', 'salary']] # First 5 rows, specific columns
Filtering (This is SUPER important)
# Single condition
df[df['age'] > 25]
df[df['city'] == 'Mumbai']
df[df['salary'] >= 50000]
# Multiple conditions (AND)
df[(df['age'] > 25) & (df['city'] == 'Mumbai')]
# Multiple conditions (OR)
df[(df['city'] == 'Mumbai') | (df['city'] == 'Delhi')]
# Using isin() for multiple values
df[df['city'].isin(['Mumbai', 'Delhi', 'Bangalore'])]
# String contains
df[df['name'].[Link]('Kumar', case=False)]
# Not equal
df[df['status'] != 'Cancelled']
# Between values
df[df['price'].between(100, 500)]
# Null / Not Null
df[df['email'].isnull()] # Rows where email is missing
df[df['email'].notnull()] # Rows where email exists
5. Data Cleaning (Most Important Skill)
Real data is messy. Here's how to clean it:
Handling Missing Values
# Check missing values
[Link]().sum()
# Drop rows with any missing values
[Link]()
# Drop rows where specific columns are missing
[Link](subset=['email', 'phone'])
# Fill missing values
df['salary'].fillna(0) # Fill with zero
df['salary'].fillna(df['salary'].mean()) # Fill with mean
df['category'].fillna('Unknown') # Fill with text
[Link](method='ffill') # Forward fill
Removing Duplicates
# Check for duplicates
[Link]().sum()
# Remove duplicates
df.drop_duplicates()
# Remove duplicates based on specific columns
df.drop_duplicates(subset=['email'])
# Keep last occurrence instead of first
df.drop_duplicates(subset=['email'], keep='last')
Fixing Data Types
# Convert to numeric
df['price'] = pd.to_numeric(df['price'], errors='coerce')
# Convert to datetime
df['date'] = pd.to_datetime(df['date'])
# Convert to string
df['id'] = df['id'].astype(str)
# Convert to category (saves memory for repeated values)
df['status'] = df['status'].astype('category')
Renaming & Formatting
# Rename columns
[Link](columns={'old_name': 'new_name'})
# Rename multiple columns
[Link](columns={
'cust_nm': 'customer_name',
'prd_id': 'product_id'
})
# Clean column names (lowercase, remove spaces)
[Link] = [Link]().[Link](' ', '_')
# Strip whitespace from string columns
df['name'] = df['name'].[Link]()
# Uppercase/lowercase
df['city'] = df['city'].[Link]() # Mumbai, Delhi
6. Data Manipulation
Creating New Columns
# Simple calculation
df['total'] = df['quantity'] * df['price']
# Conditional column
df['status'] = [Link](df['score'] >= 50, 'Pass', 'Fail')
# Multiple conditions
df['grade'] = [Link](
[df['score'] >= 90, df['score'] >= 80, df['score'] >= 70],
['A', 'B', 'C'],
default='F'
)
# From existing columns
df['full_name'] = df['first_name'] + ' ' + df['last_name']
# Extract from datetime
df['year'] = df['date'].[Link]
df['month'] = df['date'].[Link]
df['day_name'] = df['date'].dt.day_name()
Grouping & Aggregating (Key for Analysis)
# Basic groupby
[Link]('category')['sales'].sum()
# Multiple aggregations
[Link]('category')['sales'].agg(['sum', 'mean', 'count'])
# Group by multiple columns
[Link](['region', 'category'])['sales'].sum()
# Different aggregations for different columns
[Link]('category').agg({
'sales': 'sum',
'quantity': 'mean',
'customer_id': 'nunique' # Count unique customers
})
# Reset index after groupby
[Link]('category')['sales'].sum().reset_index()
Sorting
# Sort by one column
df.sort_values('sales') # Ascending
df.sort_values('sales', ascending=False) # Descending
# Sort by multiple columns
df.sort_values(['region', 'sales'], ascending=[True, False])
# Sort and get top N
[Link](10, 'sales') # Top 10 by sales
[Link](10, 'price') # Bottom 10 by price
7. Merging DataFrames
Combining data from multiple sources is essential:
# Inner join (only matching rows)
merged = [Link](df1, df2, on='customer_id')
# Left join (all from left, matching from right)
merged = [Link](df1, df2, on='customer_id', how='left')
# Right join
merged = [Link](df1, df2, on='customer_id', how='right')
# Outer join (all rows from both)
merged = [Link](df1, df2, on='customer_id', how='outer')
# Join on different column names
merged = [Link](df1, df2,
left_on='cust_id',
right_on='customer_id')
# Concatenate (stack vertically)
combined = [Link]([df1, df2])
# Concatenate horizontally
combined = [Link]([df1, df2], axis=1)
8. Data Visualization
Visualizing data helps you understand it better and communicate insights:
Quick Pandas Plots
# Line plot
df['sales'].plot()
# Bar chart
df['category'].value_counts().plot(kind='bar')
# Histogram
df['age'].plot(kind='hist', bins=20)
# Box plot
df['salary'].plot(kind='box')
# Scatter plot
[Link](kind='scatter', x='age', y='salary')
Matplotlib (More Control)
import [Link] as plt
# Create figure
fig, ax = [Link](figsize=(10, 6))
# Bar chart
categories = [Link]('category')['sales'].sum()
[Link](kind='bar', ax=ax, color='steelblue')
# Customize
ax.set_title('Sales by Category', fontsize=14)
ax.set_xlabel('Category')
ax.set_ylabel('Total Sales')
[Link](rotation=45)
plt.tight_layout()
# Save
[Link]('sales_chart.png', dpi=300)
[Link]()
Seaborn (Beautiful Charts)
import seaborn as sns
# Bar plot with automatic aggregation
[Link](data=df, x='category', y='sales')
# Count plot (frequency)
[Link](data=df, x='category')
# Box plot by category
[Link](data=df, x='category', y='salary')
# Scatter with trend line
[Link](data=df, x='age', y='salary')
# Heatmap (for correlation)
[Link](figsize=(10, 8))
[Link]([Link](), annot=True, cmap='coolwarm')
# Distribution plot
[Link](df['salary'], kde=True)
# Pair plot (multiple variables)
[Link](df[['age', 'salary', 'experience']])
9. Saving Your Work
# Save to CSV
df.to_csv('[Link]', index=False)
# Save to Excel
df.to_excel('[Link]', index=False, sheet_name='Data')
# Save multiple sheets to Excel
with [Link]('[Link]') as writer:
df1.to_excel(writer, sheet_name='Sales', index=False)
df2.to_excel(writer, sheet_name='Summary', index=False)
# Save to JSON
df.to_json('[Link]', orient='records')
10. Practice Project: Sales Analysis
Here's a complete mini-project you can do with any sales dataset:
import pandas as pd
import [Link] as plt
import seaborn as sns
# 1. Load data
df = pd.read_csv('sales_data.csv')
# 2. Explore
print(f"Shape: {[Link]}")
print([Link]())
print([Link]())
# 3. Clean
[Link](subset=['customer_id'], inplace=True)
df['date'] = pd.to_datetime(df['date'])
df['revenue'] = df['quantity'] * df['price']
# 4. Analysis
# Top 10 products by revenue
top_products = [Link]('product')['revenue'].sum().nlargest(10)
print("Top 10 Products:\n", top_products)
# Monthly trend
df['month'] = df['date'].dt.to_period('M')
monthly = [Link]('month')['revenue'].sum()
# 5. Visualize
fig, axes = [Link](2, 2, figsize=(14, 10))
# Chart 1: Top products
top_products.plot(kind='barh', ax=axes[0,0], color='teal')
axes[0,0].set_title('Top 10 Products by Revenue')
# Chart 2: Monthly trend
[Link](ax=axes[0,1], marker='o')
axes[0,1].set_title('Monthly Revenue Trend')
# Chart 3: Category distribution
df['category'].value_counts().plot(kind='pie', ax=axes[1,0], autopct='%1.1f%%')
axes[1,0].set_title('Sales by Category')
# Chart 4: Revenue distribution
[Link](df['revenue'], ax=axes[1,1], kde=True)
axes[1,1].set_title('Revenue Distribution')
plt.tight_layout()
[Link]('sales_analysis.png', dpi=300)
[Link]()
# 6. Save summary
summary = [Link]('category').agg({
'revenue': 'sum',
'quantity': 'sum',
'customer_id': 'nunique'
}).rename(columns={'customer_id': 'unique_customers'})
summary.to_excel('sales_summary.xlsx')
print("Analysis complete!")
Quick Reference Cheat Sheet
Task Code
Load CSV pd.read_csv("[Link]")
Load Excel pd.read_excel("[Link]")
First 5 rows [Link]()
Shape [Link]
Column types [Link]
Statistics [Link]()
Missing values [Link]().sum()
Filter rows df[df["col"] > value]
Select columns df[["col1", "col2"]]
Group & sum [Link]("col")["val"].sum()
Sort df.sort_values("col", ascending=False)
Remove duplicates df.drop_duplicates()
Fill nulls df["col"].fillna(value)
Merge [Link](df1, df2, on="col")
Save CSV df.to_csv("[Link]", index=False)
■ What's Next?
1. Download a dataset from Kaggle
2. Load it using pandas
3. Explore with head(), info(), describe()
4. Clean the data
5. Do some groupby analysis
6. Create 2-3 visualizations
7. Save your findings
That's it. That's how real data analysts work. Now go practice! ■