The PY function in Excel runs Python code inside a cell as part of Python in Excel. You type =PY( in a cell, write the Python, and it returns an Excel value or a Python object to the grid. Use it when Python gives a clearly better path than standard formulas.
The Complete Excel Course with AI Integration
From formulas and pivot tables to Power Query, DAX, VBA and AI with Copilot, ChatGPT and Claude — hands-on from the first lesson.
By Sagnik Bhattacharya and Virginia Thorn, Primetime Emmy Award winner
Buy nowThat does not mean every workbook should suddenly become Python-heavy. The useful question is where the PY function earns its place.
When should you use the PY function in Excel?
Use the PY function when Python gives you a clearly better analytical or transformation path than standard Excel formulas. Keep ordinary spreadsheet logic in formulas when that remains the simpler and more transparent choice.
- A task is easier in Python than in formulas.
- The workbook already has a sensible table structure.
- You can still explain the result to colleagues who may not write Python themselves.
Why use the PY function in Excel?
The PY function is useful because it lowers the friction between spreadsheet work and Python-based analysis. That helps when you want richer data handling without leaving the workbook environment.
How does the PY function work?
The PY function takes two arguments, python_code and return_type, where 0 returns an Excel value and 1 returns a Python object. You do not type those arguments yourself: Excel's Python editor writes the PY formula into the cell for you. The code runs on a secure Microsoft Cloud runtime, and the xl() function brings in ranges, names and tables.
Where does the PY function earn its place?
The PY function is strongest for analysis, transformation, and calculations that would be awkward or brittle in formulas. It is weaker when a plain formula is already readable and stable.
If you want to build the formula side of your workbooks with an instructor, try The Complete Excel Course with AI Integration on Coding Liquids.
How do you keep PY workbooks team-friendly?
Keep a PY workbook team-friendly by using Python only where the gain is obvious, documenting what each Python cell does, and not turning an otherwise simple workbook into a puzzle for the next person.
The Complete Excel Course with AI Integration
From formulas and pivot tables to Power Query, DAX, VBA and AI with Copilot, ChatGPT and Claude — hands-on from the first lesson.
By Sagnik Bhattacharya and Virginia Thorn, Primetime Emmy Award winner
Buy nowWorked example: grouping and plotting
A workbook needs a grouped analysis with a quick visual. The PY function helps pull the table into Python logic for the analysis step, then returns the result in a way the rest of the workbook can still use.
Common mistakes
- Using PY to show technical cleverness.
- Skipping workbook documentation around what the Python logic is doing.
- Ignoring whether colleagues can support the workbook later.
When should you use something other than PY?
Choose ordinary formulas over PY when a plain formula already reads clearly, because spreadsheet logic stays simpler for colleagues who do not write Python.
If you want the broader entry point first, start with Python in Excel for beginners. If you need AI help on top of Python workflows, Copilot in Excel with Python is the next step.
The Complete Excel Course with AI Integration
From formulas and pivot tables to Power Query, DAX, VBA and AI with Copilot, ChatGPT and Claude — hands-on from the first lesson.
By Sagnik Bhattacharya and Virginia Thorn, Primetime Emmy Award winner
Buy nowFrequently asked questions
What is the PY function?
The PY function runs Python inside an Excel cell via Python in Excel, returning results or objects into the grid. It takes python_code and a return_type of 0 (Excel value) or 1 (Python object), and the code runs on Microsoft's cloud. You get Python-grade analysis without leaving the workbook.
When should I reach for PY over a formula?
Reach for PY when Python is clearly the better path: richer statistics, complex transformation, plotting or modelling. Keep ordinary spreadsheet logic in formulas where they are simpler and more transparent. If a colleague cannot follow a PY cell easily, a formula is usually the better choice for that part of the workbook.
Where does PY earn its place?
PY earns its place in analysis and transformations that would be awkward or brittle as formulas: data cleaning, group-wise statistics, regressions, and charts from libraries like pandas and matplotlib. It is weaker where a plain formula already reads cleanly, so use it for the hard parts and leave the simple ones alone.
How do I keep PY workbooks team-friendly?
Use Python only where the gain is obvious, comment what each cell does, and do not turn a simple workbook into a puzzle. The next person should still be able to follow it. Documenting the purpose of each PY cell and keeping the sheet's table structure sensible makes the workbook easier to support later.
Where does the Python actually run?
In Python in Excel, the code runs in Microsoft's secure cloud runtime rather than on your machine, and returns results to the grid. That affects performance, data residency, and which libraries are available, so it is worth knowing before you rely on it.
What are PY's limits?
PY cannot be combined with other Excel functions in the same formula. It is weaker when a plain formula already reads cleanly, and it depends on the cloud runtime and a supported Excel build. Check availability and any data-handling constraints before using it for sensitive or offline work.
Related guides on this site
If you want to keep going without opening dead ends, these are the most useful next reads from this site.
- The Complete Excel Guide 2026: Formulas, AI Copilot, and Modern M365
- Python in Excel for Beginners: The First 10 Things Worth Learning
- Copilot in Excel With Python: Forecasting, Risk Analysis, and Deeper Reasoning
- Build a Forecasting Model in Excel With AI Assistance Step by Step
- How to Use Copilot in Excel to Analyse Data
Official references
These official references are useful if you need the product or framework documentation alongside this guide.
