Most data science tutorials teach you everything except what you actually need at 11pm before a deadline: the exact syntax for a pivot table, the formula for precision vs. recall, the right pandas method when merge gives you duplicates. This data science cheat sheet is built for that moment — a scannable reference covering Python data manipulation, statistics, machine learning algorithms, and visualization, with code you can copy directly.
Bookmark this page. It will save you more time than another 40-hour course.
Data Science Cheat Sheet: Python & Pandas Essentials
Pandas handles 80% of day-to-day data wrangling. These are the commands you'll reach for constantly.
Loading & Inspecting Data
import pandas as pd
import numpy as np
df = pd.read_csv('data.csv')
df.head(10) # first 10 rows
df.info() # dtypes + null counts
df.describe() # summary stats for numerics
df.shape # (rows, cols)
df.dtypes # column types
df.nunique() # unique values per column
df.isnull().sum() # null count per column
Filtering & Selecting
# Select columns
df[['col1', 'col2']]
# Filter rows
df[df['age'] > 30]
df[(df['age'] > 30) & (df['city'] == 'NYC')]
# iloc (position) vs loc (label)
df.iloc[0:5, 0:3] # rows 0-4, cols 0-2
df.loc[0:5, 'col_name'] # rows 0-5, named col
# Query syntax (readable alternative)
df.query("age > 30 and city == 'NYC'")
Cleaning & Transforming
# Drop nulls / fill nulls
df.dropna()
df.fillna(0)
df['col'].fillna(df['col'].median())
# Rename columns
df.rename(columns={'old_name': 'new_name'}, inplace=True)
# Apply a function
df['new_col'] = df['col'].apply(lambda x: x * 2)
# String operations
df['name'].str.lower()
df['name'].str.contains('John')
df['email'].str.split('@').str[1] # extract domain
# Type conversion
df['date'] = pd.to_datetime(df['date'])
df['price'] = pd.to_numeric(df['price'], errors='coerce')
Grouping & Aggregating
# Basic groupby
df.groupby('category')['sales'].sum()
df.groupby('category').agg({'sales': 'sum', 'price': 'mean'})
# Pivot table
pd.pivot_table(df, values='sales', index='region',
columns='category', aggfunc='sum', fill_value=0)
# Value counts
df['status'].value_counts(normalize=True) # proportions
Joins
# SQL-style joins
pd.merge(df1, df2, on='id', how='left') # left join
pd.merge(df1, df2, on='id', how='inner') # inner join
pd.merge(df1, df2, left_on='user_id', right_on='id')
# Concatenate rows
pd.concat([df1, df2], ignore_index=True)
# Concatenate columns
pd.concat([df1, df2], axis=1)
Statistics Formulas Every Data Scientist Needs
Descriptive Stats
- Mean: sum of values / count
- Median: middle value when sorted (robust to outliers)
- Mode: most frequent value
- Variance: average squared deviation from mean
- Standard deviation: square root of variance
- IQR: Q3 − Q1 (use for outlier detection: flag values outside 1.5×IQR)
import numpy as np
from scipy import stats
arr = df['col'].dropna()
np.mean(arr)
np.median(arr)
stats.mode(arr)
np.std(arr, ddof=1) # ddof=1 for sample std
np.percentile(arr, [25, 75]) # Q1, Q3
Hypothesis Testing — Which Test to Use
- t-test: comparing means of 2 groups (continuous, ~normal)
- ANOVA: comparing means of 3+ groups
- Chi-squared: association between 2 categorical variables
- Mann-Whitney U: non-parametric alternative to t-test
from scipy.stats import ttest_ind, chi2_contingency
# Two-sample t-test
t_stat, p_val = ttest_ind(group_a, group_b)
# p < 0.05: reject null (groups differ significantly)
# Chi-squared
chi2, p, dof, expected = chi2_contingency(contingency_table)
Correlation
df.corr() # Pearson (linear)
df.corr(method='spearman') # Spearman (rank-based, non-linear)
# Single pair
from scipy.stats import pearsonr, spearmanr
r, p = pearsonr(df['x'], df['y'])
Machine Learning Algorithm Quick Reference
This data science cheat sheet covers the go-to algorithms and when to reach for each.
Regression (predicting continuous values)
- Linear Regression: baseline; assumes linear relationship. Check residuals.
- Ridge / Lasso: regularized linear regression. Use when features are correlated (Ridge) or you need feature selection (Lasso).
- Random Forest Regressor: works well out of the box, handles non-linearity, gives feature importance.
- Gradient Boosting (XGBoost, LightGBM): typically best performance on tabular data. Tune carefully to avoid overfitting.
Classification (predicting categories)
- Logistic Regression: fast baseline, interpretable coefficients, works when features are ~linearly separable.
- Decision Tree: interpretable, overfits easily. Rarely use alone — use as base in ensembles.
- Random Forest: good default choice. Low variance, handles missing values better than most.
- XGBoost: state-of-the-art for structured data competitions. Requires careful hyperparameter tuning.
- SVM: strong for high-dimensional data (text). Slow on large datasets.
- k-NN: lazy learner, good for anomaly detection; struggles at scale.
Clustering (unsupervised)
- k-Means: fast, assumes spherical clusters. Choose k with elbow method or silhouette score.
- DBSCAN: finds arbitrarily shaped clusters, handles noise. No need to specify k.
- Hierarchical clustering: useful when you need a dendrogram to choose the number of clusters.
Model Evaluation Metrics
from sklearn.metrics import (
accuracy_score, precision_score, recall_score,
f1_score, roc_auc_score, mean_squared_error, r2_score
)
# Classification
accuracy_score(y_true, y_pred)
precision_score(y_true, y_pred) # TP / (TP + FP)
recall_score(y_true, y_pred) # TP / (TP + FN)
f1_score(y_true, y_pred) # harmonic mean of P & R
roc_auc_score(y_true, y_proba) # use y_proba, not y_pred
# Regression
mean_squared_error(y_true, y_pred, squared=False) # RMSE
r2_score(y_true, y_pred) # 1.0 = perfect fit
Rule of thumb: use precision when false positives are costly (spam filter), recall when false negatives are costly (cancer detection), and F1 when you need a balance.
scikit-learn Pipeline Pattern
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
pipe = Pipeline([
('scaler', StandardScaler()),
('clf', RandomForestClassifier(n_estimators=100, random_state=42))
])
scores = cross_val_score(pipe, X, y, cv=5, scoring='f1')
print(f"F1: {scores.mean():.3f} ± {scores.std():.3f}")
Data Visualization Quick Commands
import matplotlib.pyplot as plt
import seaborn as sns
# Distribution
sns.histplot(df['col'], kde=True)
sns.boxplot(x='category', y='value', data=df)
# Correlation heatmap
sns.heatmap(df.corr(), annot=True, fmt='.2f', cmap='coolwarm')
# Scatter with regression line
sns.regplot(x='feature', y='target', data=df)
# Count by category
sns.countplot(x='status', data=df, order=df['status'].value_counts().index)
plt.tight_layout()
plt.savefig('plot.png', dpi=150)
plt.show()
Top Courses to Build the Skills Behind This Cheat Sheet
Cheat sheets get you unstuck — courses give you the mental model to know which command to reach for. These are the highest-rated options for building real data science fluency.
Python for Data Science, AI & Development by IBM
Covers pandas, NumPy, and data wrangling directly — the foundations behind every snippet in this cheat sheet. IBM's curriculum maps closely to what hiring managers actually test in interviews.
Tools for Data Science
Strong on the tooling layer: Jupyter, Git, Watson Studio, and the broader ecosystem. If you've memorized pandas syntax but struggle to set up a reproducible environment, this fills that gap fast.
Introduction to Data Analytics
Good entry point if you're not yet fluent in the statistics sections of this cheat sheet — hypothesis testing, correlation, and descriptive stats are covered with worked examples rather than just definitions.
Analyze Data to Answer Questions
Part of the Google Data Analytics Certificate. Particularly strong on translating business questions into the right aggregation and filtering patterns — exactly the skills behind the groupby and pivot sections above.
Process Data from Dirty to Clean
Focuses entirely on data cleaning, which is where most real projects spend 60-70% of time. If the cleaning section of this cheat sheet is where you struggle most, start here.
Python Data Science (EDX)
Academic-track course with stronger coverage of statistical theory than most bootcamp-style alternatives — useful if you want to understand why the formulas work, not just how to apply them.
FAQ
What should a data science cheat sheet include?
A practical data science cheat sheet should cover four areas: data manipulation (pandas, NumPy), statistics (descriptive stats, hypothesis tests, correlation), machine learning (algorithm selection, evaluation metrics, sklearn patterns), and visualization (matplotlib/seaborn). The most useful cheat sheets include runnable code snippets, not just concept lists.
What Python libraries do I need to know for data science?
The core stack is pandas (data manipulation), NumPy (numerical operations), scikit-learn (ML), matplotlib and seaborn (visualization), and scipy (statistical tests). For large datasets add Polars or Dask. For gradient boosting add XGBoost or LightGBM. That covers 90% of real-world data science work.
How is precision different from recall, and when does it matter?
Precision = out of everything the model labeled positive, how many actually were positive. Recall = out of all actual positives, how many did the model catch. Precision matters when false positives are expensive (sending a spam email to inbox costs little; incorrectly flagging a legitimate transaction as fraud costs a lot). Recall matters when false negatives are expensive (missing a cancer diagnosis is far worse than a false alarm).
When should I use Random Forest vs XGBoost?
Random Forest is a better first move: it has fewer hyperparameters, is harder to overfit accidentally, and gives reliable results with default settings. XGBoost often outperforms it with tuning but requires more careful cross-validation and early stopping. If you're prototyping quickly, start with Random Forest. If you're optimizing for a competition or production model, tune XGBoost.
What's the difference between iloc and loc in pandas?
iloc is position-based (integer index), like array slicing. loc is label-based (uses the actual index values). They diverge when your DataFrame has a non-default index — e.g. after a groupby or reset_index, df.loc[0] fetches the row where the index label is 0, not necessarily the first row. When in doubt, reset the index first and use iloc.
How do I handle missing values in a real project?
It depends on why they're missing. Values missing completely at random: impute with median (numeric) or mode (categorical). Values missing not at random (e.g. high earners skip income field): impute with a model or create a separate binary indicator column. Tree-based models handle NaN natively with some implementations (XGBoost, LightGBM) — verify before imputing. Never drop rows unless the proportion is small (<5%) and the missingness is random.
Bottom Line
This data science cheat sheet covers the commands and formulas that show up repeatedly in real work — not a comprehensive textbook, but a working reference for the 20% of syntax that handles 80% of problems. The pandas wrangling patterns, sklearn pipeline structure, and metric selection rules are the highest-leverage things to have internalized.
If you're building toward a data science role: get comfortable with the cleaning and aggregation sections first, since that's what most entry-level interviews test on. Then layer in the ML evaluation metrics — knowing the difference between accuracy and AUC before explaining it in a technical screen matters more than memorizing algorithm internals.
The courses listed above are the fastest path from "I reference this cheat sheet constantly" to "I know why it works and can adapt when it doesn't."