Python for Data Science
Complete Reference & Cheat Sheet
NumPy · Pandas · Matplotlib · Scikit-learn · Data Cleaning · Statistics
This reference guide is designed for data analysts, data scientists, and Python developers who want a
comprehensive, structured resource covering the most essential libraries, patterns, and techniques for data
science work. From data loading and cleaning through statistical analysis, visualization, and machine learning
— everything you need in one place.
Updated for Python 3.11+ | 2024 Edition
1. Environment Setup and Essential Imports
1.1 Installing Core Libraries
pip install numpy pandas matplotlib seaborn scikit-learn scipy jupyter
1.2 Standard Import Aliases
import numpy as np import pandas as pd import [Link] as plt import seaborn as sns
from sklearn import * from scipy import stats
Note: These aliases are universal conventions across the data science community. Always use them for
readability and compatibility with tutorials and documentation.
2. NumPy — Numerical Computing
2.1 Creating Arrays
a = [Link]([1, 2, 3, 4, 5]) # 1D array b = [Link]([[1,2,3],[4,5,6]]) # 2D array z =
[Link]((3, 4)) # All zeros o = [Link]((2, 3)) # All ones r = [Link](0, 10, 2) #
[0,2,4,6,8] l = [Link](0, 1, 5) # 5 evenly spaced from 0-1 e = [Link](3) # 3x3 identity
matrix rand = [Link](3, 3) # Random values 0-1 norm = [Link](100) #
Standard normal distribution
2.2 Array Operations
[Link] # Dimensions tuple [Link] # Data type [Link] # Number of dimensions [Link] # Total
elements # Math operations (element-wise) a + b, a - b, a * b, a / b [Link](a), [Link](a),
[Link](a) [Link](a), [Link](a), [Link](a) # Aggregations [Link](a), [Link](a), [Link](a)
[Link](a), [Link](a), [Link](a) [Link](a, [25, 50, 75])
2.3 Array Slicing and Indexing
a[0] # First element a[-1] # Last element a[1:4] # Slice index 1 to 3 a[::2] # Every second
element b[0, 1] # Row 0, column 1 (2D) b[:, 1] # All rows, column 1 b[b > 3] # Boolean
indexing [Link](a > 2, a, 0) # Conditional replacement
2.4 Reshaping and Stacking
[Link](2, 5) # Change shape (must be compatible) [Link]() # Flatten to 1D
[Link](b) # Swap axes [Link]([a, b]) # Stack vertically [Link]([a, b]) # Stack
horizontally [Link]([a, b], axis=0)
3. Pandas — Data Manipulation
3.1 Creating and Loading DataFrames
# Create from dict df = [Link]({'name': ['Alice','Bob'], 'age': [25, 30]}) # Load from
files df = pd.read_csv('[Link]') df = pd.read_excel('[Link]', sheet_name='Sheet1') df =
pd.read_json('[Link]') df = pd.read_sql('SELECT * FROM table', connection) # Read with
options df = pd.read_csv('[Link]', sep=';', # Delimiter encoding='utf-8', # Encoding
na_values=['N/A', ''],# Custom NA markers dtype={'col': str}, # Force column types
parse_dates=['date']) # Auto-parse dates
3.2 Exploring DataFrames
[Link] # (rows, columns) [Link] # Column data types [Link]() # Summary including nulls
[Link]() # Statistical summary [Link](10) # First 10 rows [Link](5) # Last 5 rows
[Link](5) # Random 5 rows [Link]() [Link] [Link]() # Count unique values
per column df.value_counts() # Frequency count (Series) [Link]().sum() # Missing values
per column
3.3 Selecting Data
df['col'] # Select column (Series) df[['col1','col2']] # Select multiple columns [Link][0] #
Row by label [Link][0] # Row by integer position [Link][0, 'col'] # Specific cell by label
[Link][0:5, 1:3] # Rows 0-4, cols 1-2 # Filtering df[df['age'] > 25] df[(df['age'] > 20) &
(df['name'] == 'Alice')] df[df['city'].isin(['NYC', 'LA'])]
df[df['name'].[Link]('Ali', na=False)]
3.4 Data Cleaning
# Handle missing values [Link]() # Drop rows with any NaN [Link](subset=['col']) #
Drop rows where col is NaN [Link](0) # Fill NaN with 0 df['col'].fillna(df['col'].mean())
[Link]() # Forward fill [Link]() # Backward fill # Remove duplicates
df.drop_duplicates() df.drop_duplicates(subset=['col']) # Rename and reorder
[Link](columns={'old': 'new'}) [Link](columns=['unwanted_col'])
df.reset_index(drop=True)
3.5 Transformations and GroupBy
# Apply functions df['col'].apply(lambda x: x * 2) [Link](str) # Apply to all cells #
GroupBy [Link]('city')['sales'].sum() [Link]('city').agg({'sales': 'sum', 'orders':
'count'}) [Link](['city','year'])['revenue'].mean() # Pivot tables pd.pivot_table(df,
values='sales', index='region', columns='year', aggfunc='sum', fill_value=0) # Merge / Join
[Link](df1, df2, on='id', how='left') [Link]([df1, df2], ignore_index=True)
4. Matplotlib & Seaborn — Data Visualization
4.1 Basic Plot Types with Matplotlib
fig, ax = [Link](figsize=(10, 6)) # Line plot [Link](x, y, color='blue', linewidth=2,
label='Series 1') # Scatter plot [Link](x, y, c='red', s=50, alpha=0.7) # Bar chart
[Link](categories, values, color='steelblue') # Histogram [Link](data, bins=30,
edgecolor='black') # Box plot [Link]([group1, group2, group3]) # Formatting
ax.set_title('Chart Title', fontsize=14) ax.set_xlabel('X Axis Label') ax.set_ylabel('Y Axis
Label') [Link]() [Link](True, alpha=0.3) plt.tight_layout() [Link]('[Link]',
dpi=150, bbox_inches='tight') [Link]()
4.2 Seaborn for Statistical Plots
# Distribution plots [Link](data=df, x='value', hue='group', kde=True)
[Link](data=df, x='category', y='value') [Link](data=df, x='group', y='score')
# Relationship plots [Link](data=df, x='x', y='y', hue='label')
[Link](data=df, x='date', y='value', hue='region') [Link](data=df, x='x', y='y')
# With regression line # Categorical plots [Link](data=df, x='category', y='value',
ci=95) [Link](data=df, x='category') # Heatmap (great for correlation matrices) corr
= [Link]() [Link](corr, annot=True, cmap='coolwarm', fmt='.2f')
5. Statistical Analysis with SciPy
5.1 Descriptive Statistics
from scipy import stats import numpy as np data = [Link]([...]) # Your data
[Link](data) # Count, mean, variance, skew, kurtosis [Link](data) # Most
frequent value [Link](data, [25, 50, 75]) [Link](data) # Standardize to
z-scores
5.2 Hypothesis Testing
# T-tests t_stat, p_val = stats.ttest_1samp(data, popmean=0) # One-sample t_stat, p_val =
stats.ttest_ind(group1, group2) # Independent t_stat, p_val = stats.ttest_rel(before, after)
# Paired # Chi-square test chi2, p, dof, expected =
stats.chi2_contingency(contingency_table) # ANOVA f_stat, p_val = stats.f_oneway(group1,
group2, group3) # Correlation r, p_val = [Link](x, y) # Pearson rho, p_val =
[Link](x, y) # Spearman (non-parametric)
6. Scikit-learn — Machine Learning
6.1 The Universal ML Workflow
from sklearn.model_selection import train_test_split from [Link] import
StandardScaler from [Link] import accuracy_score, mean_squared_error # 1. Split
data X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2,
random_state=42) # 2. Scale features scaler = StandardScaler() X_train =
scaler.fit_transform(X_train) X_test = [Link](X_test) # Use same scaler! # 3.
Train model (same API for all models) [Link](X_train, y_train) # 4. Predict predictions =
[Link](X_test) # 5. Evaluate print(accuracy_score(y_test, predictions))
6.2 Common Algorithms
# Classification from sklearn.linear_model import LogisticRegression from [Link]
import DecisionTreeClassifier from [Link] import RandomForestClassifier,
GradientBoostingClassifier from [Link] import SVC from [Link] import
KNeighborsClassifier # Regression from sklearn.linear_model import LinearRegression, Ridge,
Lasso from [Link] import RandomForestRegressor from [Link] import SVR #
Clustering (unsupervised) from [Link] import KMeans, DBSCAN,
AgglomerativeClustering # Dimensionality Reduction from [Link] import PCA
from [Link] import TSNE
6.3 Model Evaluation
from [Link] import ( # Classification accuracy_score, precision_score,
recall_score, f1_score, roc_auc_score, classification_report, confusion_matrix, # Regression
mean_squared_error, mean_absolute_error, r2_score ) # Cross-validation from
sklearn.model_selection import cross_val_score scores = cross_val_score(model, X, y, cv=5,
scoring='accuracy') print(f'CV Accuracy: {[Link]():.3f} +/- {[Link]():.3f}') #
Hyperparameter tuning from sklearn.model_selection import GridSearchCV grid =
GridSearchCV(model, param_grid, cv=5, scoring='accuracy') [Link](X_train, y_train)
best_model = grid.best_estimator_
7. Data Cleaning Patterns and Best Practices
7.1 Handling Outliers
# IQR method Q1 = df['col'].quantile(0.25) Q3 = df['col'].quantile(0.75) IQR = Q3 - Q1
df_clean = df[~((df['col'] < Q1 - 1.5*IQR) | (df['col'] > Q3 + 1.5*IQR))] # Z-score method
from scipy import stats df_clean = df[([Link]([Link](df['col'])) < 3)]
7.2 Encoding Categorical Variables
# Label encoding (ordinal) from [Link] import LabelEncoder le =
LabelEncoder() df['encoded'] = le.fit_transform(df['category']) # One-hot encoding
df_encoded = pd.get_dummies(df, columns=['category'], drop_first=True) # Ordinal encoding
with custom order from [Link] import OrdinalEncoder oe =
OrdinalEncoder(categories=[['low','medium','high']]) df['size_encoded'] =
oe.fit_transform(df[['size']])
7.3 Feature Engineering Tips
• Create interaction features: multiply or combine related columns
• Extract datetime components: year, month, day, weekday, hour
• Bin continuous variables into categories using [Link]() or [Link]()
• Apply log transformation to skewed numerical features
• Create lag features for time series data
Quick Reference Card
Task Library Key Function
Array creation NumPy [Link](), [Link](), [Link]()
Statistical ops NumPy [Link](), [Link](), [Link]()
Load CSV Pandas pd.read_csv()
Filter rows Pandas df[df['col'] > value]
Group and aggregate Pandas [Link]().agg()
Missing values Pandas [Link](), [Link]()
Line/bar/scatter Matplotlib [Link](), [Link](), [Link]()
Statistical plots Seaborn [Link](), [Link]()
Train/test split Scikit-learn train_test_split()
Standardize Scikit-learn StandardScaler().fit_transform()
Evaluate model Scikit-learn accuracy_score(), r2_score()
T-test SciPy stats.ttest_ind()
Correlation SciPy [Link]()
— End of Reference Guide —