Complete Code Examples and Cheat Sheet
Table of Contents
1. Python Basics for Data Science
2. NumPy - Numerical Computing
3. Pandas - Data Manipulation
4. Data Visualization with Matplotlib & Seaborn
5. Data Cleaning and Preprocessing
6. Machine Learning with Scikit-learn
7. Statistical Analysis
8. File Handling and APIs
9. Best Practices and Common Patterns
10. Troubleshooting Common Errors
Python Basics for Data Science {#python-basics}
Essential Data Structures
python
, # Lists - Ordered, mutable collection
data_points = [1, 2, 3, 4, 5]
mixed_data = ['John', 25, 85.5, True]
# List operations
data_points.append(6) # Add element
data_points.extend([7, 8]) # Add multiple elements
filtered = [x for x in data_points if x > 3] # List comprehension
# Dictionaries - Key-value pairs (like JSON)
student = {
'name': 'Alice',
'age': 22,
'grades': [85, 92, 78],
'major': 'Data Science'
}
# Dictionary operations
student['gpa'] = 3.85 # Add new key
grades = student.get('grades', []) # Safe access
keys = list(student.keys()) # Get all keys
Essential Functions and Control Structures
python
, # Functions with default parameters
def calculate_mean(numbers, round_digits=2):
"""Calculate mean of a list of numbers"""
if not numbers:
return None
return round(sum(numbers) / len(numbers), round_digits)
# Lambda functions (useful for data transformations)
square = lambda x: x ** 2
numbers = [1, 2, 3, 4, 5]
squared = list(map(square, numbers)) # [1, 4, 9, 16, 25]
# Conditional logic
def categorize_grade(score):
if score >= 90:
return 'A'
elif score >= 80:
return 'B'
elif score >= 70:
return 'C'
else:
return 'F'
# Loops for data processing
data = [85, 92, 78, 95, 88]
total = 0
for score in data:
total += score
average = total / len(data)
# Enumerate for index tracking
for index, score in enumerate(data):
print(f"Student {index + 1}: {score}")
NumPy - Numerical Computing {#numpy}
Array Creation and Basic Operations
python
, import numpy as np
# Creating arrays
arr1d = np.array([1, 2, 3, 4, 5])
arr2d = np.array([[1, 2, 3], [4, 5, 6]])
zeros = np.zeros((3, 4)) # 3x4 array of zeros
ones = np.ones((2, 3)) # 2x3 array of ones
random_arr = np.random.randn(1000) # 1000 random numbers (normal distribution)
# Array properties
print(f"Shape: {arr2d.shape}") # (2, 3)
print(f"Data type: {arr1d.dtype}") # int64
print(f"Number of dimensions: {arr2d.ndim}") # 2
print(f"Total elements: {arr2d.size}") # 6
Mathematical Operations
python
# Element-wise operations
arr = np.array([1, 2, 3, 4, 5])
squared = arr ** 2 # [1, 4, 9, 16, 25]
doubled = arr * 2 # [2, 4, 6, 8, 10]
sqrt_arr = np.sqrt(arr) # Square roots
# Statistical operations
data = np.random.normal(100, 15, 1000) # Normal distribution (mean=100, std=15)
mean_val = np.mean(data)
median_val = np.median(data)
std_val = np.std(data)
percentiles = np.percentile(data, [25, 50, 75])
# Array aggregations
matrix = np.random.randint(1, 10, (4, 3))
row_sums = np.sum(matrix, axis=1) # Sum along columns (for each row)
col_means = np.mean(matrix, axis=0) # Mean along rows (for each column)
Advanced Array Operations
python