How I Use ChatGPT for Data Analysis Instead of Coding
I use ChatGPT for data analysis by describing the result I want and checking the code it writes. Here that covers merging Excel files into a report with a chart, an interactive plot, a Streamlit dashboard, a refactor, a Power Query version and an SQL query. The skill that matters is describing the output precisely.
Table of contents
About the examples
The sample data is simple on purpose, because the prompts are the point and not the analysis. I am not a full-time data analyst: my own work is automating internal workflows with Python and VBA.
If you are still deciding which of the two to learn, my guide to choosing between Python and VBA compares them.
Excel report
The task: a folder holds several Excel files with the same layout and financial data for different dates. I want one consolidated table, cleaned, grouped, and written to an Excel report with a chart. I start in a Jupyter Notebook, where I can test each step instead of running one long script.
There are two ways to prompt for this. One long prompt describes the whole result and hopes for a script that works at once. I prefer small subtasks, so I can check each piece of code before the next one builds on it.
I give ChatGPT context first: “As a data analyst, I need your help with Python. Please provide efficient, readable, and Pythonic code that follows best practices.” Then I ask it to consolidate the spreadsheets in a folder called “data”. It returns code with an explanation of what it does.

Once the data is consolidated, I ask for more: the source file name in an extra column, then removing duplicates and missing values, then the total revenue, expenses and profit per category.
For the report I name the xlsxwriter package in the prompt. After I add the import at the top, the code writes a new Excel file with the grouped data and a native Excel chart.
import xlsxwriter
# category_data is the grouped DataFrame with Revenue, Expenses and Profit per category
# Create an Excel report
report_name = 'category_report.xlsx'
workbook = xlsxwriter.Workbook(report_name)
worksheet = workbook.add_worksheet()
# Write the category data to the report
worksheet.write(0, 0, 'Category')
worksheet.write(0, 1, 'Revenue')
worksheet.write(0, 2, 'Expenses')
worksheet.write(0, 3, 'Profit')
row = 1
for index, row_data in category_data.iterrows():
worksheet.write(row, 0, index)
worksheet.write(row, 1, row_data['Revenue'])
worksheet.write(row, 2, row_data['Expenses'])
worksheet.write(row, 3, row_data['Profit'])
row += 1
# Create a chart representing the data
chart = workbook.add_chart({'type': 'column'})
chart.add_series({
'name': 'Revenue',
'categories': ['Sheet1', 1, 0, row - 1, 0],
'values': ['Sheet1', 1, 1, row - 1, 1],
})
chart.add_series({
'name': 'Expenses',
'categories': ['Sheet1', 1, 0, row - 1, 0],
'values': ['Sheet1', 1, 2, row - 1, 2],
})
chart.add_series({
'name': 'Profit',
'categories': ['Sheet1', 1, 0, row - 1, 0],
'values': ['Sheet1', 1, 3, row - 1, 3],
})
chart.set_title({'name': 'Total Revenue, Expenses, and Profit by Category'})
chart.set_x_axis({'name': 'Category'})
chart.set_y_axis({'name': 'Amount'})
chart.set_legend({'position': 'bottom'})
worksheet.insert_chart('E1', chart)
# Close the workbook
workbook.close()
print(f'Report saved to {report_name}.')
A newer example, where GPT-4o merges several workbooks and builds an interactive heatmap, is in automating Excel with Python and GPT-4o.
Interactive plot
ChatGPT can also write an interactive plot with Plotly and save it as an HTML file. When I run the code, the new HTML file has a title and a legend. I can hover over the bars and switch individual KPIs on and off.
Streamlit dashboard
The same approach builds a whole dashboard with Streamlit. ChatGPT generates the app code and I run it locally to show the KPIs as a web app. That is overkill for a simple analysis, but it shows how far a prompt can go.
For comparison, I built one by hand in my Streamlit sales dashboard tutorial.

Refactoring
The code works but is not clean, so I ask ChatGPT to refactor it: more Pythonic, with functions, and with pathlib instead of os. The result puts every step in its own function, which makes it easier to debug.
The refactor also fixed a problem I did not ask about. The first version combined the files with DataFrame.append, which pandas 2.0 removed. The new version uses pd.concat. If pathlib is new to you, start with my pathlib quickstart guide.
import pandas as pd
import xlsxwriter
from pathlib import Path
def load_excel_files(folder):
"""Load all Excel files in a folder into a list of dataframes"""
excel_files = list(folder.glob('*.xlsx'))
return [pd.read_excel(file) for file in excel_files]
def combine_dataframes(dataframes):
"""Combine multiple dataframes into a single dataframe"""
combined_data = pd.concat(dataframes)
return combined_data.drop_duplicates().dropna()
def calculate_category_data(combined_data):
"""Calculate total revenue, expenses, and profit for each category"""
category_data = combined_data.groupby('Category').agg({'Revenue': 'sum', 'Expenses': 'sum'})
category_data['Profit'] = category_data['Revenue'] - category_data['Expenses']
return category_data
def create_category_report(category_data, report_name):
"""Create an Excel report summarizing category data"""
workbook = xlsxwriter.Workbook(report_name)
worksheet = workbook.add_worksheet()
# Write the category data to the report
worksheet.write_row(0, 0, ['Category', 'Revenue', 'Expenses', 'Profit'])
for i, (index, row_data) in enumerate(category_data.iterrows()):
worksheet.write_row(i+1, 0, [index, row_data['Revenue'], row_data['Expenses'], row_data['Profit']])
# Create a chart representing the data
chart = workbook.add_chart({'type': 'column'})
chart.add_series({
'name': 'Revenue',
'categories': ['Sheet1', 1, 0, len(category_data), 0],
'values': ['Sheet1', 1, 1, len(category_data), 1],
})
chart.add_series({
'name': 'Expenses',
'categories': ['Sheet1', 1, 0, len(category_data), 0],
'values': ['Sheet1', 1, 2, len(category_data), 2],
})
chart.add_series({
'name': 'Profit',
'categories': ['Sheet1', 1, 0, len(category_data), 0],
'values': ['Sheet1', 1, 3, len(category_data), 3],
})
chart.set_title({'name': 'Total Revenue, Expenses, and Profit by Category'})
chart.set_x_axis({'name': 'Category'})
chart.set_y_axis({'name': 'Amount'})
chart.set_legend({'position': 'bottom'})
worksheet.insert_chart('E1', chart)
# Close the workbook
workbook.close()
print(f'Report saved to {report_name}.')
if __name__ == '__main__':
data_folder = Path('data')
excel_dataframes = load_excel_files(data_folder)
combined_data = combine_dataframes(excel_dataframes)
category_data = calculate_category_data(combined_data)
create_category_report(category_data, 'category_report.xlsx')
Power Query alternative
The same job can be done in Power Query instead of Python. I ask ChatGPT for the steps and apply them: get data from a folder, combine and transform it, add a custom column for profit, and create a pivot chart. The result matches the Python version.

SQL query
ChatGPT also writes SQL. I describe the tables (sales, customers and products) and ask it to group the sales by product and sort by total sales amount. I run the query it returns and get the totals per product.
SELECT p.product_name, SUM(s.total_price) as total_sales
FROM sales s
JOIN products p ON s.product_id = p.product_id
GROUP BY p.product_name
ORDER BY total_sales DESC;
Analysis inside the chat
ChatGPT can also run the Python itself on a file you upload, with no code on your side. I tested this with Code Interpreter, then a beta feature, on an Excel file of supermarket sales across branches. The setup and limits below describe that beta. Data analysis is now part of ChatGPT, and which features you get depends on your plan.
- Data check. I asked whether the data was clean. It found one empty column and no duplicates, and the data types were right.
- Insights. I asked for the top five. They came from basic calculations: which branch had the most sales, which product line sold best, that most customers paid in cash, and which branch had the best average rating.
- Charts. The first charts were small and had no download links. Asking for professional charts for a presentation gave me separate charts with download links, though the look barely changed.
- Recommendations. It suggested expanding the best product lines, looking into why one branch did best and promoting the popular payment methods. Basic, but a fair starting point.
- PDF report. My first attempt at a report produced the charts without the text.
In the beta, the sandbox accepted only CSV and Excel files, could not connect to databases, and had 2 GB of RAM, which is not enough for large datasets. I collected sample prompts for your own exploration. If you would rather keep the analysis on your own computer, PandasAI and Microsoft’s Data Formulator run locally with a model of your choice.
Tips
Do not run code you do not understand, and do not paste passwords or internal data into the chat. A basic grasp of the language lets you check the code quickly.
The first answer is often not what you wanted. Reword the request or split a large task into smaller steps, then piece the results together and refactor at the end.
ChatGPT inside Excel
If you would rather stay in the workbook, my MyToolBelt add-in puts ChatGPT into Excel: ask a question, or have it look at a range, and the answer appears in your sheet. It uses your own OpenAI key and runs on Windows only.
The MyToolBelt AI Companion examples show what that looks like, and the add-in is on the MyToolBelt page.

