Introduction
Data science is the backbone of modern decision-making, and Python's Pandas library is its workhorse. Whether you're cleaning messy datasets, generating insight-rich visualizations with Matplotlib and Seaborn, or performing complex aggregations, having a set of well-crafted prompts can save hours of trial and error. This article provides 15 practical, battle-tested prompts for common data science tasks. Each prompt includes a clear description, a ready-to-use code example, and an explanation of the result. By the end, you'll have a toolkit you can apply immediately to your own projects.
1. Load and Inspect a Dataset
Task: Quickly load a CSV file and understand its structure.
Prompt: "Load the dataset from 'data.csv' and display the first 5 rows, column names, data types, and basic statistics."
Code:
import pandas as pd
df = pd.read_csv('data.csv')
print('First 5 rows:')
print(df.head())
print('\nColumn names:', df.columns.tolist())
print('\nData types:')
print(df.dtypes)
print('\nBasic statistics:')
print(df.describe(include='all'))
Result: You get an immediate overview of the data: missing values are visible in describe(), numeric columns show mean and quartiles, and object columns display unique counts. This is the first step in any EDA (Exploratory Data Analysis) pipeline.
2. Handle Missing Values
Task: Identify and fill or drop missing values intelligently.
Prompt: "Check for missing values in the DataFrame. For numeric columns, fill missing values with the median. For categorical columns, fill with the mode. Then drop rows where more than 50% of the values are missing."
Code:
# Check missing values
print('Missing values per column:')
print(df.isnull().sum())
# Fill numeric with median
numeric_cols = df.select_dtypes(include=['float64', 'int64']).columns
df[numeric_cols] = df[numeric_cols].fillna(df[numeric_cols].median())
# Fill categorical with mode
cat_cols = df.select_dtypes(include=['object']).columns
for col in cat_cols:
mode_val = df[col].mode()[0] if not df[col].mode().empty else 'Unknown'
df[col] = df[col].fillna(mode_val)
# Drop rows with >50% missing
df = df.dropna(thresh=len(df.columns) * 0.5)
print('\nShape after cleaning:', df.shape)
Result: A clean DataFrame with no missing values, ready for modeling or visualization. This approach avoids introducing bias (using median) and respects categorical distributions (using mode).
3. Filter Rows Based on Conditions
Task: Extract rows that meet specific criteria.
Prompt: "Filter the DataFrame to keep only rows where 'age' is greater than 30 and 'salary' is above the median salary. Show the result sorted by salary descending."
Code:
median_salary = df['salary'].median()
filtered = df[(df['age'] > 30) & (df['salary'] > median_salary)]
filtered_sorted = filtered.sort_values('salary', ascending=False)
print(filtered_sorted.head(10))
Result: A subset of high-earning older individuals, sorted by income. This is useful for targeted analysis, like customer segmentation or outlier detection.
4. Group and Aggregate Data
Task: Summarize data by categories.
Prompt: "Group the data by 'department' and calculate the mean salary, median age, and count of employees in each department. Sort by count descending."
Code:
summary = df.groupby('department').agg(
mean_salary=('salary', 'mean'),
median_age=('age', 'median'),
count=('employee_id', 'count')
).reset_index()
summary = summary.sort_values('count', ascending=False)
print(summary)
Result: A compact table showing which departments are largest and how compensation and age vary. This is a classic business intelligence query.
5. Create a New Column Based on Existing Data
Task: Derive a feature using a formula or condition.
Prompt: "Create a new column 'age_group' that categorizes age into 'Young' (0-30), 'Middle' (31-50), and 'Senior' (51+). Also create a 'bonus' column equal to 10% of salary for employees with more than 5 years of experience."
Code:
import numpy as np
df['age_group'] = pd.cut(df['age'], bins=[0, 30, 50, np.inf], labels=['Young', 'Middle', 'Senior'])
df['bonus'] = np.where(df['experience_years'] > 5, df['salary'] * 0.1, 0)
print(df[['age', 'age_group', 'salary', 'bonus']].head())
Result: New columns that encode business logic directly into the DataFrame, enabling further analysis like total bonus cost per department.
6. Merge Two DataFrames
Task: Combine data from different sources.
Prompt: "Merge the main DataFrame 'df_employees' with a second DataFrame 'df_departments' on the 'dept_id' column. Use a left join to keep all employees. Show the first 5 rows of the merged result."
Code:
df_merged = df_employees.merge(df_departments, on='dept_id', how='left')
print(df_merged.head())
Result: A unified dataset with department names attached to each employee. This is essential when data is normalized across multiple tables.
7. Pivot Table for Summary Statistics
Task: Create a cross-tabulation of two categorical variables.
Prompt: "Create a pivot table showing the average salary by department and age_group. Fill missing values with 0."
Code:
pivot = pd.pivot_table(df, values='salary', index='department', columns='age_group', aggfunc='mean', fill_value=0)
print(pivot)
Result: A matrix that reveals salary patterns across departments and age groups. For example, you might see that the Engineering department pays Seniors significantly more.
8. Visualize Distribution with Histogram
Task: Understand the spread of a numeric variable using Matplotlib.
Prompt: "Plot a histogram of 'salary' with 30 bins, overlaid with a kernel density estimate (KDE). Add a vertical line at the mean salary."
Code:
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 6))
plt.hist(df['salary'], bins=30, density=True, alpha=0.6, color='steelblue', label='Histogram')
df['salary'].plot(kind='kde', color='red', label='KDE')
plt.axvline(df['salary'].mean(), color='green', linestyle='--', label=f'Mean: {df["salary"].mean():.2f}')
plt.xlabel('Salary')
plt.ylabel('Density')
plt.title('Salary Distribution')
plt.legend()
plt.show()
Result: A visual representation of salary concentration. If the distribution is right-skewed, it suggests a few high earners pull the mean upward.
9. Correlation Heatmap with Seaborn
Task: Explore relationships between numeric features.
Prompt: "Compute the correlation matrix of all numeric columns and plot a heatmap using Seaborn with annotations."
Code:
import seaborn as sns
corr = df.select_dtypes(include=[np.number]).corr()
plt.figure(figsize=(10, 8))
sns.heatmap(corr, annot=True, cmap='coolwarm', fmt='.2f', linewidths=0.5)
plt.title('Correlation Heatmap')
plt.show()
Result: A color-coded matrix highlighting strong positive correlations (e.g., between 'experience_years' and 'salary') and potential multicollinearity issues.
10. Bar Plot for Categorical Comparison
Task: Compare a numeric metric across categories using Seaborn.
Prompt: "Create a bar plot showing the average salary by department. Add error bars representing the standard deviation."
Code:
plt.figure(figsize=(12, 6))
sns.barplot(x='department', y='salary', data=df, ci='sd', palette='Set2')
plt.xticks(rotation=45)
plt.title('Average Salary by Department with SD')
plt.tight_layout()
plt.show()
Result: A clear comparison of average salaries across departments, with error bars indicating variability. This helps identify departments with high inequality.
11. Time Series Line Plot
Task: Visualize trends over time.
Prompt: "Assuming a 'date' column, set it as index and plot the weekly average of 'sales' over the full time range. Use Matplotlib and add grid lines."
Code:
df['date'] = pd.to_datetime(df['date'])
df.set_index('date', inplace=True)
weekly_sales = df['sales'].resample('W').mean()
plt.figure(figsize=(12, 5))
plt.plot(weekly_sales.index, weekly_sales.values, marker='o', linestyle='-', color='darkorange')
plt.grid(True, alpha=0.3)
plt.xlabel('Date')
plt.ylabel('Weekly Average Sales')
plt.title('Sales Trend Over Time')
plt.show()
Result: A line chart that reveals seasonality, trends, and potential anomalies. For instance, a sharp drop may indicate a data collection issue or a real event.
12. Box Plot to Detect Outliers
Task: Identify outliers in a numeric variable grouped by a category.
Prompt: "Create a box plot of 'salary' across 'department' using Seaborn. Highlight outliers as points beyond the whiskers."
Code:
plt.figure(figsize=(12, 6))
sns.boxplot(x='department', y='salary', data=df, palette='Set3')
plt.xticks(rotation=45)
plt.title('Salary Distribution by Department with Outliers')
plt.tight_layout()
plt.show()
Result: Box plots show median, quartiles, and outliers. For example, the 'Executive' department might have many high-salary outliers.
13. Pair Plot for Multivariate Exploration
Task: Get a quick overview of relationships between multiple numeric variables.
Prompt: "Create a pair plot of the numeric columns 'age', 'salary', 'experience_years', and 'bonus'. Color points by 'department'. Use Seaborn's pairplot."
Code:
sns.pairplot(df, vars=['age', 'salary', 'experience_years', 'bonus'], hue='department', diag_kind='kde', palette='husl')
plt.suptitle('Pair Plot of Numeric Features by Department', y=1.02)
plt.show()
Result: A grid of scatter plots and histograms that reveals clusters, trends, and interactions. You might see that Engineering has a tight salary-age relationship.
14. Customize Plot with Multiple Subplots
Task: Combine several views in one figure for a report.
Prompt: "Create a 2x2 subplot figure: top-left: histogram of salary; top-right: box plot of salary by department; bottom-left: bar plot of employee count by department; bottom-right: scatter plot of age vs salary colored by department."
Code:
fig, axes = plt.subplots(2, 2, figsize=(14, 10))
# Top-left
axes[0, 0].hist(df['salary'], bins=30, color='steelblue', edgecolor='white')
axes[0, 0].set_title('Salary Histogram')
# Top-right
sns.boxplot(x='department', y='salary', data=df, ax=axes[0, 1], palette='Set2')
axes[0, 1].set_title('Salary by Department')
axes[0, 1].tick_params(axis='x', rotation=45)
# Bottom-left
df['department'].value_counts().plot(kind='bar', ax=axes[1, 0], color='coral')
axes[1, 0].set_title('Employee Count by Department')
axes[1, 0].tick_params(axis='x', rotation=45)
# Bottom-right
scatter = axes[1, 1].scatter(df['age'], df['salary'], c=pd.factorize(df['department'])[0], cmap='viridis', alpha=0.6)
axes[1, 1].set_title('Age vs Salary')
axes[1, 1].set_xlabel('Age')
axes[1, 1].set_ylabel('Salary')
plt.tight_layout()
plt.show()
Result: A single figure that summarizes key aspects of the data — distribution, group differences, and relationships — perfect for a quick executive summary.
15. Export Cleaned Data
Task: Save the processed DataFrame for later use.
Prompt: "Export the cleaned DataFrame to a CSV file named 'cleaned_data.csv' without the index. Also save it as a Parquet file for faster loading."
Code:
df.to_csv('cleaned_data.csv', index=False)
df.to_parquet('cleaned_data.parquet', index=False)
print('Data exported successfully.')
Result: Two files ready for sharing or for use in other pipelines. Parquet is especially efficient for large datasets, reducing file size and load time significantly.
Conclusion
These 15 prompts cover the essential tasks every data scientist encounters: from loading and cleaning data to advanced visualization and export. By mastering these patterns, you can streamline your workflow and focus on extracting insights rather than wrestling with syntax. The examples are designed to be modular — mix and match them to fit your specific dataset. Next time you start a new analysis, open your notebook and run these prompts. Your future self will thank you.
Note: All code examples assume Python 3.8+ with Pandas 1.5+, Matplotlib 3.5+, and Seaborn 0.12+. For official documentation, visit pandas.pydata.org, matplotlib.org, and seaborn.pydata.org.
Comments