Data analysis is the backbone of data science. Whether you're cleaning messy datasets, exploring patterns, or building visualizations, having a structured approach can save hours of trial and error. This article presents 12 carefully crafted prompts designed to accelerate your workflow with Pandas, Matplotlib, and Seaborn. Each prompt targets a common real-world task, provides a reusable template, and includes a working code example. By the end, you'll have a toolkit of prompts you can adapt to nearly any dataset.
Why Prompts Matter in Data Science
Prompts act as mental scaffolds. They break a complex task (e.g., "analyze customer churn") into concrete, actionable steps. In data science, a prompt typically includes: a clear objective, data assumptions, expected outputs, and optional parameters. Using prompts reduces cognitive load, improves reproducibility, and helps teams collaborate more effectively. The examples below use the classic Titanic dataset (available from Seaborn) because it's familiar, but the same logic applies to any tabular data.
Basic Prompts (1–4): Data Loading, Cleaning, and Summary Statistics
Prompt 1: Load and Inspect Data Quickly
- Task: Load a CSV file and get a first look at its structure.
- Prompt:
"""
Load the dataset from 'data.csv' into a Pandas DataFrame. Display the first 5 rows, the shape (rows, columns), and a concise summary of column data types and missing values.
""" - Example Result:
```python
import pandas as pd
import seaborn as sns
df = sns.load_dataset('titanic')
print('Shape:', df.shape)
print(df.head())
print(df.info())
```
Output: shape (891, 15), info shows dtypes and non-null counts.
Prompt 2: Identify and Handle Missing Values
- Task: Detect missing values and decide how to treat them.
- Prompt:
"""
Compute the percentage of missing values per column. For numeric columns with < 5% missing, fill with the median. For categorical columns with < 5% missing, fill with the mode. Drop columns with > 50% missing. Return the cleaned DataFrame.
""" - Example Result:
python missing_pct = df.isnull().mean() * 100 print(missing_pct) # Example: age (19.87%), embarked (0.22%), deck (77.22%) -> drop deck df_clean = df.drop(columns=['deck']) df_clean['age'].fillna(df_clean['age'].median(), inplace=True) df_clean['embarked'].fillna(df_clean['embarked'].mode()[0], inplace=True)
Prompt 3: Generate Summary Statistics with a Single Line
- Task: Quickly obtain descriptive statistics for all numeric columns.
- Prompt:
"""
Using the cleaned DataFrame, generate a table of summary statistics: count, mean, std, min, 25%, 50%, 75%, max. Also include the number of unique values for categorical columns.
""" - Example Result:
python print(df_clean.describe(include='all')) # Shows stats for numeric columns and top/freq/unique for objects
Prompt 4: Detect Outliers Using IQR
- Task: Flag rows that contain outliers in a given numeric column.
- Prompt:
"""
For the column 'fare', calculate the first quartile (Q1), third quartile (Q3), and IQR. Define outliers as values below Q1 - 1.5IQR or above Q3 + 1.5IQR. Return a new column 'fare_outlier' with boolean values.
""" - Example Result:
python Q1 = df_clean['fare'].quantile(0.25) Q3 = df_clean['fare'].quantile(0.75) IQR = Q3 - Q1 df_clean['fare_outlier'] = (df_clean['fare'] < Q1 - 1.5*IQR) | (df_clean['fare'] > Q3 + 1.5*IQR) print(df_clean['fare_outlier'].value_counts())
Intermediate Prompts (5–8): Transformation, Grouping, and Merging
Prompt 5: Pivot Tables for Aggregation
- Task: Create a pivot table to compare survival rates by passenger class and sex.
- Prompt:
"""
Build a pivot table with 'pclass' as index, 'sex' as columns, and the mean of 'survived' as values. Format the result as percentages rounded to 2 decimal places.
""" - Example Result:
python pivot = df_clean.pivot_table(index='pclass', columns='sex', values='survived', aggfunc='mean') pivot = pivot * 100 print(pivot.round(2)) # Output: sex female male # pclass # 1 96.81 36.89 # 2 92.31 15.74 # 3 50.00 13.54
Prompt 6: Grouped Operations with Multiple Aggregations
- Task: For each combination of 'embarked' and 'pclass', compute count, average age, and median fare.
- Prompt:
"""
Group the data by ['embarked', 'pclass']. Use the agg() method to compute: - 'survived': mean (survival rate)
- 'age': mean
- 'fare': median
Reset index and rename columns for clarity.
""" - Example Result:
python grouped = df_clean.groupby(['embarked', 'pclass']).agg( survival_rate=('survived', 'mean'), avg_age=('age', 'mean'), median_fare=('fare', 'median') ).reset_index() print(grouped.round(3))
Prompt 7: Apply Custom Functions with .apply()
- Task: Create a new column 'age_group' that categorizes ages into 'Child', 'Teen', 'Adult', 'Senior'.
- Prompt:
"""
Write a function that returns age group based on age: <13 -> Child, 13-19 -> Teen, 20-64 -> Adult, >=65 -> Senior. Use .apply() to map it to each row. Handle missing ages by assigning 'Unknown'.
""" - Example Result:
python def age_to_group(age): if pd.isna(age): return 'Unknown' elif age < 13: return 'Child' elif age < 20: return 'Teen' elif age < 65: return 'Adult' else: return 'Senior' df_clean['age_group'] = df_clean['age'].apply(age_to_group) print(df_clean['age_group'].value_counts())
Prompt 8: Merge Two DataFrames on a Common Key
- Task: Merge the Titanic passenger data with a separate dataset containing passenger names and ticket classes (simulate a second table).
- Prompt:
"""
Create a second DataFrame with columns 'passenger_id' and 'cabin_letter' (first letter of cabin). Perform a left join on 'passenger_id'. Handle non-matching rows.
""" - Example Result:
python cabin_df = df_clean[['passenger_id', 'cabin']].copy() cabin_df['cabin_letter'] = cabin_df['cabin'].str[0] merged = df_clean.merge(cabin_df[['passenger_id', 'cabin_letter']], on='passenger_id', how='left') print(merged[['passenger_id', 'cabin_letter']].head())
Advanced Prompts (9–12): Visualization, Statistical Analysis, and ML Prep
Prompt 9: Distribution Plot with Seaborn
- Task: Visualize the distribution of age and survival status.
- Prompt:
"""
Using Seaborn, create a histogram of 'age' with hue='survived', kernel density estimate (KDE) overlay, and a semi-transparent style. Set a professional color palette (e.g., 'Set2').
""" - Example Result:
python import matplotlib.pyplot as plt import seaborn as sns sns.set_theme(style='whitegrid', palette='Set2') plt.figure(figsize=(10,6)) sns.histplot(data=df_clean, x='age', hue='survived', kde=True, alpha=0.6) plt.title('Age Distribution by Survival') plt.show()
Prompt 10: Correlation Heatmap for Feature Selection
- Task: Display a heatmap of correlations between numeric features to identify multicollinearity.
- Prompt:
"""
Compute the Pearson correlation matrix for all numeric columns. Use Seaborn heatmap with annotations, a divergent color map (coolwarm), and a mask for the upper triangle to avoid redundancy.
""" - Example Result:
python import numpy as np corr = df_clean.select_dtypes(include=[np.number]).corr() mask = np.triu(np.ones_like(corr, dtype=bool)) plt.figure(figsize=(10,8)) sns.heatmap(corr, mask=mask, annot=True, cmap='coolwarm', fmt='.2f', center=0) plt.title('Correlation Heatmap') plt.show()
Prompt 11: Statistical Test (Chi-Square for Independence)
- Task: Test if survival is independent of passenger class.
- Prompt:
"""
Create a contingency table of 'survived' vs 'pclass'. Run a chi-square test of independence using scipy.stats. Print the chi-square statistic, p-value, and degrees of freedom. Interpret the result at alpha=0.05.
""" - Example Result:
python from scipy.stats import chi2_contingency contingency = pd.crosstab(df_clean['pclass'], df_clean['survived']) chi2, p, dof, expected = chi2_contingency(contingency) print(f'Chi2 = {chi2:.2f}, p-value = {p:.4f}, df = {dof}') # Conclusion: p < 0.05 → reject null; survival depends on class.
Prompt 12: Prepare Data for Machine Learning (One-Hot Encoding)
- Task: Convert categorical columns into dummy variables and split into train/test sets.
- Prompt:
"""
Identify categorical columns with dtype 'object' or 'category'. Apply one-hot encoding using pd.get_dummies() with drop_first=True to avoid multicollinearity. Separate features (X) and target (y='survived'). Split into 80/20 train/test sets with random_state=42.
""" - Example Result:
python from sklearn.model_selection import train_test_split cat_cols = df_clean.select_dtypes(include=['object', 'category']).columns df_encoded = pd.get_dummies(df_clean, columns=cat_cols, drop_first=True) X = df_encoded.drop('survived', axis=1) y = df_encoded['survived'] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) print(f'Train size: {X_train.shape}, Test size: {X_test.shape}')
Conclusion
These 12 prompts provide a structured starting point for common data science tasks. From loading data to preparing it for modeling, each prompt is a reusable pattern you can adapt to your own datasets. The key is to understand the underlying logic—once you do, you can combine prompts, tweak parameters, and build more complex pipelines. Try adapting the Titanic examples to your next project, and watch your productivity soar. For further learning, explore the official Pandas documentation (pandas.pydata.org) and Seaborn gallery (seaborn.pydata.org). Happy analyzing!
Comments