How ChatGPT Can Generate Ready‑to‑Run Pandas Code for Excel Data Analysis

This article explains how to use ChatGPT to automatically generate accurate pandas scripts for Excel‑based data analysis, compares Python packages with VBA, shows prompt engineering techniques, and demonstrates adapting the code for Excel's built‑in Python feature.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
How ChatGPT Can Generate Ready‑to‑Run Pandas Code for Excel Data Analysis

In recent years Python, especially the pandas library, has begun to replace traditional VBA for Excel data analysis, and the arrival of ChatGPT has accelerated this shift.

By crafting precise prompts, users can ask ChatGPT to produce pandas code that reads an Excel file, performs group‑by calculations, and outputs results without any manual editing. An example prompt asks the model to read D:/Samples/ch03/02 各班学生成绩.xlsx, calculate each class's average score to one decimal place, and add comments.

import pandas as pd
# 读取Excel文件数据
df = pd.read_excel('D:/Samples/ch01/各班学生成绩.xlsx', engine='openpyxl')
# 计算各班学生成绩的平均分
average_scores = df.groupby('班级')['成绩'].mean().round(1)
# 输出各班平均成绩
for index, value in average_scores.items():
    print(f'{index}班 {value}')

The generated script runs directly in Python IDLE, producing the average scores for each class.

Comparing Python packages, pandas offers the highest success rate for ChatGPT‑generated code, while xlwings and OpenPyXL are more error‑prone and often require additional prompt refinement.

Excel’s built‑in Python (Beta) runs on Microsoft’s cloud, eliminating local installation issues and fully supporting pandas. The same pandas logic can be adapted for this environment with minimal changes:

df = xl("A1:C26", headers=True)
df.groupby('班级')['成绩'].mean().round(1)

When entered in an Excel cell using the =PY( formula, the result is a Series object that expands to show each class’s average score.

Beyond code generation, the article proposes building a prompt‑template library that maps typical data‑analysis problems to effective prompts, enabling systematic reuse and faster problem solving.

Overall, the recommendation is to prefer Python’s pandas for data analysis tasks, leverage ChatGPT for code generation, and use Excel’s built‑in Python to execute the scripts seamlessly.

Excel built‑in Python example
Excel built‑in Python example
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonPrompt EngineeringChatGPTExcel
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.