PY function
Runs Python code inside an Excel cell (Python in Excel). The code reads worksheet ranges with xl() and returns a value or a Python object.
Available in Microsoft 365 only (Excel for Windows, Mac and the web, depending on plan and update channel). Code runs in the Microsoft Cloud, so an internet connection is required.
Microsoft 365 onlyNot in Google Sheets
Syntax
PY(python_code, return_type)| Argument | What it means | |
|---|---|---|
python_code | Required | The Python code to run. You do not type it as a quoted string: you enter Python mode and type the code directly in the cell or formula bar. |
return_type | Required | 0 to return the result as Excel values (numbers, text, a spilled range), 1 to keep it as a Python object such as a DataFrame. You switch this with the output menu next to the formula bar. |
Returns: The value of the last line of the Python code, either converted to Excel values or as a Python object.
How PY works
PY is the function behind Python in Excel. It lets you write Python — the language used throughout data analysis — directly in a cell, with popular libraries such as pandas, NumPy, Matplotlib and statsmodels already available. You start it by typing =PY( in a cell (or choosing Formulas > Insert Python); the cell switches to Python mode and you type code instead of an Excel formula. Press Ctrl+Enter to run it.
The bridge between the sheet and Python is the xl() function. xl("A1:C6", headers=True) turns a range into a pandas DataFrame whose columns are named after the header row; from there you can filter, group, calculate statistics or draw charts. The value of the last line of your code becomes the cell's result. With the output set to Excel value it appears as normal numbers or text (tables spill); with Python object the cell shows a card such as DataFrame that other Python cells can reference.
Some important differences from normal formulas: the code does not run on your computer but in an isolated Microsoft Cloud container, so it cannot read your local files or the internet directly — data comes in through xl() only. Python cells are calculated in order, row by row from left to right across sheets, so a cell can use variables defined in Python cells above it. The formulas shown on this page are how Excel represents the code; in the cell you only see and edit the Python itself.
Examples
1Total a column with pandas
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Region | Rep | Sales | ||
| 2 | East | Ana | 5200 | 27150 | |
| 3 | West | Ben | 3100 | ||
| 4 | East | Carla | 7150 | ||
| 5 | North | Dev | 3900 | ||
| 6 | West | Fay | 7800 |
Result: 27150
In the cell you type xl("A1:C6", headers=True)["Sales"].sum() in Python mode, with the output set to Excel value. xl() loads the table as a DataFrame, ['Sales'] picks the column and .sum() adds it: 27,150 — the same as =SUM(C2:C6).
2Average sales
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Region | Rep | Sales | ||
| 2 | East | Ana | 5200 | ||
| 3 | West | Ben | 3100 | 5430 | |
| 4 | East | Carla | 7150 | ||
| 5 | North | Dev | 3900 | ||
| 6 | West | Fay | 7800 |
Result: 5430
.mean() returns 5430.0 in Python, which Excel displays as 5430. The value is identical to =AVERAGE(C2:C6); Python becomes worthwhile for things Excel formulas make hard, such as regressions, text analysis or advanced charts.
3Keep the table as a Python object
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Region | Rep | Sales | ||
| 2 | East | Ana | 5200 | ||
| 3 | West | Ben | 3100 | ||
| 4 | East | Carla | 7150 | ||
| 5 | North | Dev | 3900 | DataFrame | |
| 6 | West | Fay | 7800 |
Result: DataFrame
With the output set to Python object, the cell shows a DataFrame card instead of spilling the data. Other Python cells can refer to it (for example df = xl("E5")), and clicking the card previews its contents.
Common errors and how to fix them
| Error | Why it happens | Fix |
|---|---|---|
#PYTHON! | The Python code raised an error — a typo, a wrong column name, or a type problem. | Open the diagnostics pane (click the error icon) to read the Python error message, and check column names exactly match the headers. |
#BUSY! | The code is still being calculated in the cloud. | Wait a moment; large DataFrames and charts can take a few seconds. |
#CONNECT! | Excel cannot reach the Python service, for example when offline. | Check the internet connection and try again. |
Tips
- Use
headers=Truein xl() whenever the range has a header row; otherwise the headers are treated as data. - Point xl() at an Excel Table (
xl("SalesTable[#All]", headers=True)) so new rows are picked up automatically. - Keep heavy calculations in one Python cell and return a Python object; downstream cells then reuse it instead of reloading the data.
Google Sheets
No Python in cells; use Apps Script (JavaScript) for custom logic, or analyse the data in Colab or BigQuery.