Skip to main content
data intermediate

Generate Python Pandas Code for Data Analysis

Generate clean, efficient Python pandas code for data analysis tasks. Perfect for intermediate data analysts and developers working with DataFrames.

Works with: chatgptclaudegemini

Prompt Template

You are an expert Python data analyst specializing in pandas. Write clean, efficient, and well-commented pandas code to accomplish the following data analysis task. Task Description: [TASK_DESCRIPTION] Dataset Information: - Dataset: [DATASET_NAME] - Key columns: [COLUMN_NAMES] - Data types: [DATA_TYPES] - Dataset size: [DATASET_SIZE] Specific Requirements: [SPECIFIC_REQUIREMENTS] Please provide: 1. Complete, executable pandas code with proper imports 2. Clear comments explaining each step 3. Error handling where appropriate 4. Performance considerations if relevant 5. Brief explanation of the approach 6. Sample output or visualization code if applicable Code should follow these standards: - Use descriptive variable names - Include data validation steps - Optimize for readability and maintainability - Handle common edge cases (missing values, data type issues) - Use appropriate pandas methods for the task If the task involves multiple steps, break it down into logical sections with clear headings in comments.

Variables to Customize

[TASK_DESCRIPTION]

The specific data analysis task you want to accomplish

Example: Calculate monthly sales trends and identify the top 10 products by revenue, including seasonal analysis

[DATASET_NAME]

Name or description of your dataset

Example: e-commerce sales data (sales_data.csv)

[COLUMN_NAMES]

The main columns in your dataset that are relevant to the analysis

Example: product_id, product_name, sale_date, quantity, unit_price, category, customer_id

[DATA_TYPES]

Data types of the key columns

Example: product_id (int), product_name (str), sale_date (datetime), quantity (int), unit_price (float)

[DATASET_SIZE]

Approximate size of your dataset

Example: ~50,000 rows, 2 years of data

[SPECIFIC_REQUIREMENTS]

Any specific requirements, constraints, or output format needed

Example: Group by month and product category, handle missing values by forward-fill, export results to Excel with multiple sheets

Example Output

```python import pandas as pd import numpy as np from datetime import datetime import matplotlib.pyplot as plt # Load and validate the dataset df = pd.read_csv('sales_data.csv') print(f"Dataset loaded: {df.shape[0]} rows, {df.shape[1]} columns") # Data preprocessing df['sale_date'] = pd.to_datetime(df['sale_date']) df['revenue'] = df['quantity'] * df['unit_price'] df['month'] = df['sale_date'].dt.to_period('M') # Handle missing values df['unit_price'] = df['unit_price'].fillna(method='ffill') print(f"Missing values handled: {df.isnull().sum().sum()} remaining nulls") # Monthly sales trends analysis monthly_sales = df.groupby('month').agg({ 'revenue': 'sum', 'quantity': 'sum', 'product_id': 'nunique' }).rename(columns={'product_id': 'unique_products'}) # Top 10 products by revenue top_products = df.groupby(['product_name', 'category'])['revenue'].sum()\ .reset_index()\ .sort_values('revenue', ascending=False)\ .head(10) # Seasonal analysis df['season'] = df['sale_date'].dt.month.map({ 12: 'Winter', 1: 'Winter', 2: 'Winter', 3: 'Spring', 4: 'Spring', 5: 'Spring', 6: 'Summer', 7: 'Summer', 8: 'Summer', 9: 'Fall', 10: 'Fall', 11: 'Fall' }) seasonal_trends = df.groupby(['season', 'category'])['revenue'].sum().unstack(fill_value=0) print("Analysis complete. Results ready for export.") ```

Pro Tips for Best Results

  • Be specific about your data structure and column names - this helps generate more accurate code
  • Mention any data quality issues you're aware of so the code includes appropriate handling
  • Specify your desired output format (CSV, Excel, visualization) to get complete solutions
  • Include performance requirements if working with large datasets - this will influence the suggested approach
  • Ask for code explanations if you want to understand the pandas methods being used

Tags

Want 500+ Expert Prompts?

Get the Premium Prompt Pack — organized, tested, and ready to use.

Get it for $29

Related Prompts You Might Like