Excel course · Lesson 34 of 34
Python and Copilot in Excel: what they are and how to use them carefully
What Python in Excel and Copilot can do, how to start, which versions have them, and how to check AI-generated results before you rely on them.
Two newer additions change what Excel can do. Python in Excel lets you write Python code in a cell and use libraries such as pandas and Matplotlib on your worksheet data. Copilot is Microsoft's AI assistant: you describe what you want in plain language, and it suggests formulas, highlights, PivotTables, charts or analysis. Both are powerful, both are still evolving, and both require understanding what they do with your data. This lesson explains each, shows a first example, and gives a checklist for using them responsibly.
Python in Excel
What Python in Excel is
Python in Excel adds a PY function. Instead of an Excel formula, the cell contains Python code. The code does not run on your computer: it runs in a secure container in the Microsoft Cloud, with a fixed set of popular libraries (pandas, NumPy, Matplotlib, seaborn, statsmodels, scikit-learn and others) provided through Anaconda. Your worksheet data is sent to that container, the code runs, and the result comes back into the cell.
- Available in: Microsoft 365 subscriptions on Windows (business, enterprise, and in more recent rollouts also Family and Personal), Excel for the web, and with a later rollout in Excel for Mac. Not in Excel 2021, 2024 or other one-time-purchase versions.
- Needs an internet connection, because calculation happens in the cloud.
- Cannot read local files, access the internet, or install your own packages from within the cell. It works on data you pass in from the workbook.
- Some plans include standard compute with limits and offer faster premium compute as a paid add-on; heavy use may be slower on the standard tier.
Your first Python cell
| A | B | |
|---|---|---|
| 1 | Month | Sales |
| 2 | Jan | 42000 |
| 3 | Feb | 45500 |
| 4 | Mar | 51200 |
| 5 | Apr | 48900 |
| 6 | May | 56300 |
Monthly sales in A1:B6. We will analyse them with Python.
- Click an empty cell, for example
D2. - Choose Formulas › Python › Insert Python, or type
=PY(and pressTab, or useCtrl+Alt+Shift+P. The cell changes into Python mode: a green PY badge appears in the formula bar. - Type
df = xl("A1:B6", headers=True)and pressEnterfor a new line. Thexl()function is how Python reads workbook ranges;headers=Truemakes the first row the column names. You can also select the range with the mouse and Excel writes thexl()call for you. - On the next line type
df["Sales"].mean(). - Press
Ctrl+Enterto run the code. (In Python mode, Enter adds a new line instead of committing.) - The cell shows 48,780, the average monthly sales.
Python cells run in order: left to right, top to bottom across the sheets. A variable such as df defined in D2 can be used in any Python cell that comes later in that order. This is different from Excel formulas, where the order does not matter.
Python objects versus Excel values
A Python cell can return a Python object or an Excel value. If the last line returns a pandas DataFrame, the cell shows a small card icon labelled DataFrame; click it to preview. To spill the DataFrame into cells as a normal range, switch the output to Excel Value using the menu next to the PY badge in the formula bar (or Ctrl+Alt+Shift+M). For example, df.describe() returned as Excel values spills a table with count, mean, std, min, quartiles and max.
df = xl("A1:B6", headers=True)
df["Change %"] = df["Sales"].pct_change() * 100
df.round(1)Charts with Python
Matplotlib and seaborn charts are returned as images inside the cell. import matplotlib.pyplot as plt is already loaded, so plt.plot(df["Month"], df["Sales"]) followed by plt.show() (or just the plot as the last expression) returns a picture. Click the image icon to view it larger, or use Place in Cell / Place over Cells to make it bigger on the sheet. These images do not update their style like native charts, but they make statistical plots that Excel lacks (regression lines, pair plots, heat maps) easy.
#PYTHON!. Open the Diagnostics pane (Formulas › Python › Diagnostics in current builds) to read the full Python traceback. Typical causes: a misspelled column name, a range without headers, or text in a numeric column.Copilot in Excel
What Copilot is
Copilot is an AI assistant built into Microsoft 365. In Excel it appears as a Copilot button on the Home tab and opens a chat pane. You type a request in everyday language, such as 'add a column with the profit margin' or 'which region grew fastest?', and Copilot proposes a formula, a highlight, a sort, a PivotTable, a chart or a written answer, which you can insert or discard.
- Requirements (as broadly documented): a Microsoft 365 subscription that includes Copilot (a Microsoft 365 Copilot licence for business accounts, or a personal/family plan with Copilot features in supported regions), and in most cases a file saved to OneDrive or SharePoint with AutoSave on.
- Best data shape: a clean table or range with clear, unique headers and no merged cells. Formatting the data as an Excel table (Excel tables and structured references) helps Copilot understand it.
- Newer abilities, rolling out in stages, include multi-step edits across a workbook and using Python for 'advanced analysis', where Copilot writes and runs Python code for you.
A first Copilot request
- Format your data as a table, for example the sales list above.
- Choose Home › Copilot.
- Type:
Add a column that shows the change from the previous month in percent. - Copilot shows the proposed formula and a preview. Read the formula before clicking Insert column.
- Check two rows by hand. For February the change should be about 8.3%; for April it should be about -4.5% (48,900 / 51,200 − 1).
The COPILOT function
Microsoft has also introduced a COPILOT worksheet function, rolling out first to Insider and Beta channels, that sends a prompt plus optional cell ranges to the AI model and returns the answer into the grid, for example =COPILOT("Classify this feedback as positive, negative or neutral", B2). It is designed for language tasks such as classifying, summarising or extracting text. Microsoft itself advises against using it for numerical calculations or for tasks that must be exactly reproducible, and usage is subject to limits.
Using AI results responsibly
- Verify every formula Copilot suggests. Read it, test it on a row you can calculate by hand, and check edge cases such as blanks and zeros.
- Prefer formulas over answers: a formula in the sheet can be audited and recalculates; a sentence in a chat pane cannot.
- Results can vary: the same prompt may give a slightly different answer another time. Do not use AI output as the only basis for financial, legal or HR decisions.
- Data handling: Python and Copilot both process workbook data in Microsoft's cloud under your organisation's Microsoft 365 terms. Follow your company's policy for confidential or personal data.
- Keep skills: AI is fastest for people who can judge its output. Knowing XLOOKUP, SUMIFS and PivotTables is what lets you catch a wrong suggestion in seconds.
Related features you may already have
- Home › Analyze Data (formerly Ideas) suggests charts and PivotTables from a table, available in Microsoft 365 for some years.
- Formula suggestions and Formula by Example in Excel for the web propose formulas as you type or fill a pattern.
- Flash Fill and Column From Examples in Power Query, older pattern-learning tools that work offline (Text cleanup: TRIM, CLEAN, TEXTSPLIT, Flash Fill and Text to Columns).
Check yourself
1. Where does Python code in Excel run?
Python in Excel runs in Microsoft's cloud, so an internet connection is needed.
2. In a Python cell, which key combination runs the code?
In Python mode Enter adds a new line; Ctrl+Enter commits and runs.
3. How does a Python cell read the range A1:B6 with headers?
The xl() function brings workbook ranges into Python.
4. Copilot suggests a formula for a new column. What should you do before relying on it?
Always verify AI-generated formulas on rows you can calculate yourself.
5. What is the COPILOT worksheet function designed for?
Microsoft recommends it for text tasks, not for calculations that must be exact or reproducible.