Formulas & Excel logic
Python in Excel
The =PY() formula: referencing ranges with xl(), returning DataFrames and plotting in cells.
Start with the full guides
The long-form treatment of python in excel, with the reasoning the shorter pages assume.
Step-by-step how-tos
One task each, with code you can copy, run and adapt.
- How-toClean Data with pandas in Python in ExcelUse pandas inside Excel's =PY() cells to clean a messy table — trim text, fix types, drop duplicates, standardise categories — and return the cleaned table to the grid, with the calculation-order and output-mode rules that make it work.
- How-toForecast a Time Series with Python in ExcelUse Python in Excel to forecast monthly sales from a table in the workbook: pull the history with xl(), give it a proper monthly date index, fit a Holt-Winters model with statsmodels, return the forecast and a simple prediction band as a spilled range, chart it with matplotlib, and know the limits — cloud execution, recalculation and FORECAST.ETS as a formula-only alternative.
- How-toGet External Data into Python in Excel with Power QueryPython in Excel cannot read files or call the internet, so bring CSVs, databases and web data in with Power Query, then analyse them with xl() — tables versus connection-only queries, refresh order, parameters and size limits.
- How-toPlot Charts with matplotlib in Python in ExcelDraw matplotlib and seaborn charts inside a =PY cell, show them in the cell or floating over the grid, size them properly, and know when a native Excel chart is the better choice.
- How-toReference Cells, Ranges and Tables with xl() in Python in ExcelEverything a =PY cell knows about your workbook comes through xl(). Reference single cells, ranges with headers, Excel tables, other sheets, named ranges and Power Query results.
- How-toReturn DataFrames and Values from Python in Excel CellsA =PY cell returns its last expression, either as a Python object card or spilled into the grid as Excel values. Shape DataFrames for the grid, avoid #SPILL!, and chain cells without recomputing.
- How-toRun a Regression with Python in ExcelFit a multiple linear regression in a =PY cell with statsmodels: read a table with xl(), fit with a formula, return a tidy coefficient table to the grid, predict with intervals, check the residuals with a chart, and confirm the numbers against LINEST.
Compare the options
Which tool or approach to pick, and why.
When it breaks
The errors this topic produces, with the cause explained rather than just the fix.
Follow it through a track
Prefer an ordered route? These tracks cover python in excel along the way, in the order that builds on itself.
Related topics
Subjects that keep turning up on the same pages as this one, most overlap first.
Python in Excel is part of Formulas & Excel logic.