Analyze a Dataset With Pandas Step-by-Step
Generate step-by-step Pandas EDA code covering nulls, outliers, and a business question.
This prompt makes the model write commented, step-by-step pandas code for a real exploratory analysis on your DataFrame — profiling, null handling, IQR outlier detection, and then answering a specific business question. Reach for it when you have a dataset loaded and want a defensible analysis pipeline rather than a wall of unexplained code. Listing your actual columns and types is what lets it pick the right operations instead of guessing at your schema.
You are a data scientist. I have a pandas DataFrame called 'df' with these columns: [LIST COLUMN NAMES AND TYPES]. Write Python code to: (1) perform an EDA (shape, dtypes, null counts, basic stats), (2) identify and handle missing values, (3) detect and summarize outliers using IQR, (4) answer this specific question: '[BUSINESS QUESTION]'. Add a comment above each block explaining what it does and why.
What you can expect back
# 1. EDA — understand shape, types, and where data is missing
print(df.shape)
print(df.dtypes)
print(df.isnull().sum())
print(df.describe(include='all'))
# 2. Handle missing values — drop rows missing the key fields, flag amount nulls
df = df.dropna(subset=['region', 'refunded'])
df['amount'] = df['amount'].fillna(df['amount'].median()) # median resists outliers
# 3. Outliers in amount via IQR — summarize rather than auto-delete
q1, q3 = df['amount'].quantile([0.25, 0.75])
iqr = q3 - q1
low, high = q1 - 1.5*iqr, q3 + 1.5*iqr
outliers = df[(df['amount'] < low) | (df['amount'] > high)]
print(f"{len(outliers)} outlier orders ({len(outliers)/len(df):.1%})")
# 4. Refund rate by region
rates = df.groupby('region')['refunded'].mean().sort_values(ascending=False)
print(rates)
# Meaningfulness: compare top region's rate to overall and check sample size per region
print(df['region'].value_counts())Illustrative example — your results will vary by tool and inputs.
Get sharper results
- 01Get the exact dtypes right (especially datetime vs. object and category vs. string) because the code branches on them — a date stored as text will break any time-based step.
- 02Tell it your row count order of magnitude; for millions of rows it should suggest vectorized or sampled approaches instead of operations that quietly blow up memory.
- 03Specify whether outliers should be removed, capped, or just reported, since auto-deleting them can silently distort the very business answer you're after.
- 04Ask for the statistical test by name (e.g. a chi-square or two-proportion z-test) when the question is about whether a difference is 'meaningful,' so it quantifies significance instead of eyeballing it.
- 05Request that it print intermediate results after each block so you can sanity-check the data as the pipeline runs rather than trusting the final number blind.
Adapt it for your case
Append 'include matplotlib/seaborn plots for distributions and the final answer' to get charts alongside the tables.
Replace 'pandas DataFrame' with 'Polars DataFrame' and ask for idiomatic Polars expressions for speed on large data.
Ask it to 'wrap the pipeline in a documented function that takes df and the business question's columns as parameters.'
Common questions
Will the code run without errors on my data?
Only if your real columns and dtypes match what you described. Mismatches — a stringified date, an unexpected null — are the usual cause of breakage, so verify the schema first.
Is dropping or filling nulls the right call for my dataset?
The model picks a reasonable default, but the correct strategy depends on why data is missing. State the cause if you know it, or ask it to explain the tradeoffs of each option.
Does IQR catch all outliers?
IQR is a solid general method but assumes a roughly unimodal distribution. For skewed or multimodal data, ask it to also try z-scores or a domain-specific threshold.
You may also need
Write a SQL Query From a Business Question
Translate a business question into a clean, commented SQL query against your schema.
Write a Data Cleaning Script for Messy Data
Generate a step-by-step Pandas data cleaning script with issue detection and before/after summaries.
Use SQL Window Functions for Advanced Analytics
Generate advanced SQL window function queries with explanations and performance notes.
Turn a Plain-English Question Into a SQL Query
Converts a natural-language business question into a correct, commented SQL query grounded in your real schema.