⚡ Accelerating Data Science Workflows with Jupyter AI & Pandas
Combining Python's powerful pandas library with Jupyter AI magic commands (%ai and %%ai) allows data scientists to load datasets, handle missing values, aggregate metrics, and generate matplotlib visualizations through natural language prompts.
Example 1: Loading, Inspecting, and Summarizing CSV Datasets
Load an e-commerce sales dataset into a Pandas DataFrame and use Jupyter AI to generate descriptive statistics and summary insights.
Python (Jupyter Notebook Cell)
import pandas as pd
# Load sales dataset
df = pd.read_csv("ecommerce_sales.csv")
# Display initial rows and data info
print(df.info())
print(df.head())
# Use Jupyter AI to summarize dataset characteristics
%%ai openai:gpt-4o
Analyze the loaded pandas DataFrame 'df' (columns: order_id, customer_id, product_category, amount, order_date). Write code to output total revenue and top 3 product categories by sales volume.
Total Revenue: $1,452,890.50
Top 3 Categories by Volume:
1. Electronics ($620,400)
2. Home & Kitchen ($380,150)
3. Apparel ($245,340)
Example 2: Automated Missing Value Detection and Imputation
Detect null values across columns and apply intelligent imputation strategies using Pandas and AI-generated cleaning scripts.
Python (Jupyter Notebook Cell)
# Check missing values count per column
print(df.isnull().sum())
%%ai anthropic:claude-3-5-sonnet-20241022
Write pandas code to fill missing values in numeric column 'amount' with the median, and categorical column 'product_category' with the mode 'Unknown'. Ensure inplace=True.
Missing values successfully imputed:
- 'amount' nulls filled with median: 154.50
- 'product_category' nulls filled with 'Unknown'
DataFrame integrity verified. Zero null values remaining in critical columns.
Example 3: Multi-Level Groupby & Pivot Table Aggregation
Group transaction data by region and product category to calculate multi-metric aggregations (mean, sum, count).
Python (Jupyter Notebook Cell)
%%ai google:gemini-1.5-pro
Write pandas code to group 'df' by ['region', 'product_category'] and calculate total revenue (sum) and average order value (mean) for 'amount'. Pivot the result so regions are columns.
North America Europe Asia-Pacific
product_category
Apparel $120,400 $85,200 $39,740
Electronics $310,200 $198,500 $111,700
Home & Kitchen $190,100 $120,050 $70,000
4. Example 4: Automated Data Visualization & Plotting
Generate publication-ready Seaborn and Matplotlib bar charts directly from aggregated Pandas DataFrames using AI prompts.
Python (Jupyter Notebook Cell)
import matplotlib.pyplot as plt
import seaborn as sns
%%ai openai:gpt-4o
Write Python code using seaborn and pandas to plot a horizontal bar chart of total revenue per product category with a clean dark-blue palette and gridlines. Display the plot in Jupyter.
[Matplotlib Output Rendering]
Successfully generated horizontal bar chart displaying Electronics leading sales ($620k), followed by Home & Kitchen ($380k) and Apparel ($245k) with customized seaborn styling.
5. Example 5: Natural Language DataFrame Querying (Pandas AI Integration)
Query your Pandas DataFrame using plain English questions without writing explicit filtering syntax, powered by Jupyter AI.
Python (Jupyter Notebook Cell)
import pandasai as pai
# Initialize PandasAI smart dataframe
from pandasai import SmartDataframe
smart_df = SmartDataframe(df, config={"openai_api_key": "sk-proj-..."})
# Query DataFrame using natural language magic command
%%ai openai:gpt-4o
What is the average transaction amount for orders placed in the 'North America' region where the amount exceeds $500?
Generated Query Execution:
df[(df['region'] == 'North America') & (df['amount'] > 500)]['amount'].mean()
Result: $784.25