Underground Utilities, RAM Savers & AI Prompts
Explore the 500 tricks database one-by-one at random to discover hidden coding shortcuts!
Step-by-step roadmaps followed by industry experts, showing the exact complete code blocks copy-pasteable into any real-world project.
The step-by-step technical standard used by Senior Data Analysts in Fortune 500 tech, consulting & fintech companies—from setup and join validation to RFM customer scoring, SQL windowing, and multi-sheet Excel reporting.
Pandas Column Display Limits, Decimal Float Format & Warning Suppression
Kab & Kyun? Jab dataset me 50+ columns hon to Pandas defaults inspect karte waqt `...` se columns hide kar deta hai. `display.max_columns = None` karne se terminal aur notebook me saare columns visible rehte hain.
Kab & Kyun? Financial revenue numbers (`$1,250,450.50`) standard pandas me confusing scientific notation (`1.250450e+06`) jaisey print hote hain. `display.float_format` se exact 2-decimal money format fix ho jata hai.
Kab & Kyun? Library updates se terminal me ugly `SettingWithCopyWarning` print hone lagte hain. `warnings.filterwarnings('ignore')` production scripts ko clean aur readable rakhta hai.
import pandas as pd
import warnings
warnings.filterwarnings('ignore')
# 1.1 Configure Pandas Display Options (No Hidden Columns/Rows)
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', 100)
pd.set_option('display.width', 1000)
# 1.2 Format Floating Point Numbers to Clean Currency (2 Decimals)
pd.set_option('display.float_format', lambda x: '%.2f' % x)
print("Stage 1 Complete: Production environment & display options configured!")
CSV Encodings, Multi-Tab Excel Spreadsheets, SQL Warehouses, REST APIs & Parquet
Kab & Kyun? European/Legacy CSV files me Special Characters (`utf-8` failure) ya tab (`\t`) / pipe (`|`) delimiters hote hain. `encoding='latin1'` aur `sep='|'` crash-proof ingestion guarantee karte hain.
Kab & Kyun? Large Excel files me Q1, Q2, Q3 tabs hote hain. `pd.ExcelFile` use karne se workbook disk par 1 baar parse hoti hai aur `sheet_name=None` saare tabs ko dictionary me load kar deta hai.
Kab & Kyun? Production data Postgres/MySQL/SQLite me store hota hai. SQL query filters application layer par execute hoti hain jisse RAM me sirf filtered data load hota hai.
Kab & Kyun? Web services JSON payloads aur AWS S3 Lake Parquet files provide karti hain. `pd.read_parquet()` 10x kam memory leti hai.
import sqlite3
# 2.1 CSV with Custom Encoding & Delimiters
df_csv = pd.read_csv('sales_data.csv', encoding='latin1', sep=',')
# 2.2 Multi-Sheet Excel Workbook
excel_file = pd.ExcelFile('annual_report_2025.xlsx')
all_sheets_dict = pd.read_excel(excel_file, sheet_name=None) # Loads all tabs into dict
# 2.3 SQL Relational Database Connections
conn = sqlite3.connect('company_warehouse.db')
df_orders = pd.read_sql_query("SELECT * FROM orders WHERE status = 'COMPLETED'", conn)
df_users = pd.read_sql_query("SELECT user_id, country, signup_date FROM users", conn)
conn.close()
# 2.4 Compressed Parquet Binary Data File
df_parquet = pd.read_parquet('big_transactions.parquet')
SQL-Style Joins, Join Integrity Cardinality Checks & Unmatched Key Audits
Kab & Kyun? Orders table ko Users lookup table se `user_id` par join karna standard analytics flow hai. `how='left'` base transaction count preserve rakhta hai.
Kab & Kyun? Real-world bug: Subah target lookup table me duplicate `user_id` hone par `merge` rows duplicate kar deta hai! `validate='many_to_one'` use karne se duplicates aate hi pandas Error raise kar deta hai.
Kab & Kyun? `indicator=True` dataset me `_merge` column attach kar deta hai (`both` vs `left_only`), jisse missing customer profiles immediately identify ho jate hain.
# 3.1 & 3.2 Safe Relational Left Join with Cardinality Assertion
df = pd.merge(
df_orders,
df_users,
on='user_id',
how='left',
validate='many_to_one', # Prevents row duplication from bad lookup tables
indicator=True
)
# 3.3 Audit Unmatched Left-Only Records
unmatched_orders = (df['_merge'] == 'left_only').sum()
print(f"Join Audit: {unmatched_orders} orders have missing user lookup details.")
df = df.drop(columns=['_merge'])
Dimensions, Null Percentages, Duplicate Keys & Quality Asserts
Inspect dimensions (`df.shape`) and check if numbers are wrapped as strings (`df.info()`).
Calculate missing value ratios (`(df.isna().mean() * 100).round(2)`).
Use `assert (df['revenue'] >= 0).all()` to throw instant alerts if corrupted records enter the pipeline.
# 4.1 & 4.2 Missing Value & Datatype Audit Table
missing_table = pd.DataFrame({
'Null_Count': df.isna().sum(),
'Null_%': (df.isna().mean() * 100).round(2),
'Data_Type': df.dtypes
}).sort_values(by='Null_%', ascending=False)
print("Data Health Audit Summary:\n", missing_table[missing_table['Null_Count'] > 0])
# 4.3 Production Quality Assertions
if 'revenue' in df.columns:
assert (df['revenue'].dropna() >= 0).all(), "Data Pipeline Exception: Negative revenue values detected!"
Header Snake_case, Currency Symbols, Text Mapping & Memory Downcasting
Convert raw messy headers (`Customer Revenue ($)`) to clean `customer_revenue`.
Strip `$`, `,`, `%` characters from numeric text columns and cast with `pd.to_numeric()`.
Parse mixed timestamp strings using `pd.to_datetime(..., format='mixed')`.
# 5.1 Standardize Column Headers to Lowercase Snake_case
df.columns = (df.columns.str.strip().str.lower()
.str.replace(' ', '_')
.str.replace('[^a-z0-9_]', '', regex=True))
# 5.2 Currency & Special Symbol Removal
for col in ['revenue', 'price', 'amount', 'salary']:
if col in df.columns:
df[col] = (df[col].astype(str)
.str.replace('$', '', regex=False)
.str.replace(',', '', regex=False)
.str.replace('%', '', regex=False).str.strip())
df[col] = pd.to_numeric(df[col], errors='coerce')
# 5.3 Datetime Parsing
if 'order_date' in df.columns:
df['order_date'] = pd.to_datetime(df['order_date'], errors='coerce', format='mixed')
IQR Outlier Winsorization, Threshold Dropping & Median Imputation
Kab & Kyun? Outliers ko completely delete karne se real data drop ho jata hai. IQR Bounds ($Q_1 - 1.5\text{IQR}, Q_3 + 1.5\text{IQR}$) calculate karke `.clip()` karne se extreme values smooth ho jati hain.
Fill missing numerical gaps with `median()` instead of mean to keep estimations unbiased.
# 6.1 IQR Outlier Capping (Winsorization)
if 'revenue' in df.columns:
Q1 = df['revenue'].quantile(0.25)
Q3 = df['revenue'].quantile(0.75)
IQR = Q3 - Q1
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
df['revenue'] = df['revenue'].clip(lower=lower_bound, upper=upper_bound)
# 6.2 Threshold Column Dropping & Median Fill
df = df.dropna(thresh=int(len(df) * 0.50), axis=1)
num_cols = df.select_dtypes(include=['int32', 'int64', 'float64']).columns
for col in num_cols:
if df[col].isna().sum() > 0:
df[col] = df[col].fillna(df[col].median())
Datetime Signals, Multi-Condition Rule Engines & RFM Customer Scoring
Extract Year, Month Name, Day of Week, Quarter and `is_weekend` (0/1) flag.
Calculate Recency, Frequency, Monetary (RFM) scores to segment High-Value/At-Risk customers.
# 7.1 Datetime Signals Extraction
if 'order_date' in df.columns:
df['year'] = df['order_date'].dt.year
df['month_name'] = df['order_date'].dt.month_name()
df['is_weekend'] = df['order_date'].dt.dayofweek.isin([5, 6]).astype(int)
# 7.2 RFM Customer Segmentation (Recency, Frequency, Monetary)
if 'user_id' in df.columns and 'order_date' in df.columns and 'revenue' in df.columns:
snapshot_date = df['order_date'].max() + pd.Timedelta(days=1)
rfm = df.groupby('user_id').agg(
recency=('order_date', lambda x: (snapshot_date - x.max()).days),
frequency=('order_date', 'count'),
monetary=('revenue', 'sum')
).reset_index()
rfm['r_score'] = pd.qcut(rfm['recency'], q=4, labels=[4, 3, 2, 1])
rfm['m_score'] = pd.qcut(rfm['monetary'], q=4, labels=[1, 2, 3, 4])
print("RFM Customer Segmentation Summary:\n", rfm.head())
Lead/Lag Offsets (`shift()`), Partition Ranks (`rank()`) & Moving Averages
Calculate order-over-order differences using `shift(1)` to simulate SQL `LAG()`.
Group-specific ranking using `rank(ascending=False)` to find regional leaders.
# 8.1 SQL LAG() Equivalent
if 'user_id' in df.columns and 'revenue' in df.columns:
df = df.sort_values(by=['user_id', 'order_date'])
df['prev_order_revenue'] = df.groupby('user_id')['revenue'].shift(1)
df['rev_change'] = df['revenue'] - df['prev_order_revenue']
# 8.2 SQL RANK() OVER (PARTITION BY region ORDER BY revenue DESC)
if 'region' in df.columns and 'revenue' in df.columns:
df['region_rank'] = df.groupby('region')['revenue'].rank(ascending=False, method='min')
# 8.3 Running Total & Moving Averages
df['running_revenue'] = df.groupby('user_id')['revenue'].cumsum()
df['7d_moving_avg'] = df['revenue'].rolling(window=7, min_periods=1).mean()
Executive Aggregations, 2D Pivot Matrices & Monthly Cohort Matrices
Calculate revenue metrics, user counts and order frequencies in a single pass.
Build summary matrices with grand totals (`margins=True`) for stakeholder reporting.
# 9.1 Executive Multi-Metric Groupby Summary
executive_summary = df.groupby('region').agg(
total_revenue=('revenue', 'sum'),
average_order_val=('revenue', 'mean'),
median_order_val=('revenue', 'median'),
unique_customers=('user_id', 'nunique'),
total_orders=('revenue', 'count')
).reset_index().sort_values(by='total_revenue', ascending=False)
# 9.2 2D Pivot Matrix with Margins
pivot_matrix = pd.pivot_table(
df,
values='revenue',
index='region',
columns='spend_tier',
aggfunc='sum',
fill_value=0,
margins=True
)
Multi-Tab Excel Spreadsheets (`ExcelWriter`), Clipboard Copy & SQL Upload
Write analytical outputs into organized tabs of a single `.xlsx` workbook using `pd.ExcelWriter()`.
Copy summary tables directly to system clipboard using `to_clipboard(excel=True)` for quick Excel/Slack pasting.
# 10.1 Multi-Tab Excel Report Generation
with pd.ExcelWriter('Executive_Analytics_Report_2025.xlsx', engine='openpyxl') as writer:
executive_summary.to_excel(writer, sheet_name='Executive_Summary', index=False)
pivot_matrix.to_excel(writer, sheet_name='Regional_Pivots')
df.to_excel(writer, sheet_name='Clean_Master_Data', index=False)
print("Multi-tab Excel workbook successfully generated!")
# 10.2 Instant Clipboard Copy (Excel Ready)
executive_summary.to_clipboard(excel=True, index=False)
print("Summary copied to clipboard! Press Ctrl+V in Excel.")
# 10.3 SQL Database Table Write-Back
conn = sqlite3.connect('company_warehouse.db')
df.to_sql('clean_analytics_table', conn, if_exists='replace', index=False)
conn.close()
# 10.4 Compressed Parquet Format Export
df.to_parquet('clean_analytics_table.parquet', index=False)
print("Full 10-Stage Data Analyst Masterclass Pipeline Execution Complete!")
The production machine learning standard—from feature matrix isolation and leakage-safe ColumnTransformers to stratified CV, GridSearchCV tuning, and Joblib binary serialization for cloud APIs.
Isolating Predictor Features from Labels & Diagnostic Class Imbalance Checks
Kab & Kyun? Supervised ML models target variable ($y$) ko predict karne ke liye historical features ($X$) learn karte hain. Primary Keys (`user_id`, `transaction_id`) aur target column ko drop karna zaroori hai.
Kab & Kyun? `y.value_counts(normalize=True)` check karke identify karein ki target skewed to nahi hai (e.g., 90% Non-Churn vs 10% Churn).
import pandas as pd
# Load clean preprocessed analytics dataset
df = pd.read_csv('cleaned_master_dataset.csv')
# Define Target Variable
target_col = 'churn' # 1 = Churned, 0 = Retained
# 1.1 Separate Feature Matrix (X) and Target Vector (y)
X = df.drop(columns=[target_col, 'user_id', 'transaction_id'], errors='ignore')
y = df[target_col]
# 1.2 Target Class Ratio Diagnostic
print(f"Feature Matrix Shape X: {X.shape}")
print(f"Target Imbalance Distribution:\n{y.value_counts(normalize=True).round(3)}")
StandardScaler for Numeric & OneHotEncoder with handle_unknown='ignore'
Kab & Kyun? Logistic Regression aur Neural Nets scale sensitive hote hain. `StandardScaler` mean 0 aur standard deviation 1 par normalize kar deta hai.
Kab & Kyun? `OneHotEncoder(handle_unknown='ignore')` use karne se production API me koi naya text category aane par code crash nahi hota.
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
# Detect Numeric vs Categorical Columns
num_cols = X.select_dtypes(include=['int32', 'int64', 'float64']).columns.tolist()
cat_cols = X.select_dtypes(include=['object', 'category']).columns.tolist()
# Declarative ColumnTransformer Preprocessor
preprocessor = ColumnTransformer(
transformers=[
('num', StandardScaler(), num_cols),
('cat', OneHotEncoder(handle_unknown='ignore', drop='first'), cat_cols)
]
)
print("ColumnTransformer successfully constructed!")
Bundling Preprocessor + Estimator into a Unified Pipeline
Kab & Kyun? Split se pehle scaling karne par test dataset ka mean train me leak ho jata hai (Data Leakage!). `Pipeline` guarantee karta hai ki scaling har fold ke andar strictly train set par execute hogi.
Bundle preprocessor and RandomForest algorithm into a single reproducible object.
from sklearn.pipeline import Pipeline
from sklearn.ensemble import RandomForestClassifier
# Create Data-Leakage Safe Machine Learning Pipeline
model_pipeline = Pipeline(steps=[
('preprocessor', preprocessor),
('classifier', RandomForestClassifier(n_estimators=100, max_depth=10, random_state=42, n_jobs=-1))
])
print("Unified ML Pipeline instantiated successfully!")
80% Train vs 20% Unseen Test Set with Stratified Class Preservation
Kab & Kyun? Imbalanced data me random split se test set me 0 positive labels ho sakte hain! `stratify=y` flag train aur test me exact target ratio maintain rakhta hai.
`random_state=42` ensures identical peer-verifiable results across every run.
from sklearn.model_selection import train_test_split
# 80% Train, 20% Unseen Test Split with Target Stratification
X_train, X_test, y_train, y_test = train_test_split(
X, y,
test_size=0.20,
random_state=42,
stratify=y
)
print(f"X_train samples: {X_train.shape[0]} | X_test samples: {X_test.shape[0]}")
Executing Fit Operations and Evaluating 5-Fold Stratified CV Scores
Single `model_pipeline.fit(X_train, y_train)` fits scaling parameters and decision trees simultaneously.
Kab & Kyun? Single test split score lucky/unlucky ho sakta hai. `cross_val_score(..., cv=5)` model stability calculate karta hai.
from sklearn.model_selection import cross_val_score
# 5.1 Fit Pipeline on Training Set
model_pipeline.fit(X_train, y_train)
# 5.2 Evaluate 5-Fold Stratified Cross-Validation ROC-AUC Scores
cv_scores = cross_val_score(model_pipeline, X_train, y_train, cv=5, scoring='roc_auc', n_jobs=-1)
print(f"5-Fold CV ROC-AUC Scores: {cv_scores.round(3)}")
print(f"Mean CV ROC-AUC: {cv_scores.mean():.4f} +/- {cv_scores.std():.4f}")
Systematic Search for Optimal Tree Depths & Estimators via ROC-AUC
Search parameter dictionary (e.g., `classifier__n_estimators`, `classifier__max_depth`).
Extract `grid_search.best_estimator_` fitted with the winning hyperparameter setup.
from sklearn.model_selection import GridSearchCV
# Hyperparameter Search Space
param_grid = {
'classifier__n_estimators': [50, 100, 200],
'classifier__max_depth': [5, 10, 15, None],
'classifier__min_samples_split': [2, 5]
}
grid_search = GridSearchCV(
estimator=model_pipeline,
param_grid=param_grid,
cv=5,
scoring='roc_auc',
n_jobs=-1
)
grid_search.fit(X_train, y_train)
best_model = grid_search.best_estimator_
print("Winning Hyperparameters:", grid_search.best_params_)
print(f"Best 5-Fold Cross-Validation ROC-AUC: {grid_search.best_score_:.4f}")
Precision, Recall, F1-Score Report & ROC-AUC Probability Scoring
Evaluate Precision, Recall, and F1-score on unseen test set records (`X_test`).
Calculate class probabilities (`predict_proba()`) for threshold calibration.
from sklearn.metrics import classification_report, roc_auc_score, confusion_matrix
# Predict Labels and Class Probabilities on Unseen Test Dataset
y_pred = best_model.predict(X_test)
y_proba = best_model.predict_proba(X_test)[:, 1]
print("\n--- Model Diagnostic Report ---")
print(classification_report(y_test, y_pred))
print(f"ROC-AUC Performance Score: {roc_auc_score(y_test, y_proba):.4f}")
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
Saving Pipeline Binary (joblib) & Real-Time REST API Prediction Scoring
Kab & Kyun? `joblib.dump()` complete pipeline (preprocessor + scaler + encoder + trained model) ko single `.joblib` binary file me save karta hai.
Load serialized pipeline via `joblib.load()` and generate predictions on incoming raw JSON payloads.
import joblib
import pandas as pd
# 8.1 Serialize winning pipeline object to disk
joblib.dump(best_model, 'churn_prediction_pipeline.joblib')
print("Model pipeline saved to 'churn_prediction_pipeline.joblib'")
# 8.2 Production Real-Time Inference Script
loaded_pipeline = joblib.load('churn_prediction_pipeline.joblib')
# Incoming raw JSON payload from Web Application
new_payload = pd.DataFrame([{
'age': 34,
'revenue': 1450.00,
'region': 'North',
'spend_tier': 'Gold'
}])
# Predict churn class & probabilities
predicted_churn = loaded_pipeline.predict(new_payload)[0]
churn_prob = loaded_pipeline.predict_proba(new_payload)[0][1]
print(f"Prediction: Churn Risk = {predicted_churn} | Probability = {churn_prob:.2%}")
print("Full 8-Stage Data Scientist ML Pipeline Execution Complete!")