@KirkDBorne: Start using Python in Excel: https://support.microsoft.com/en-US/Excel/python/get-started-with-python-in-excel… + Book …

X AI KOLs Timeline Products

Summary

This article explains how to get started with Python in Excel for advanced data analysis and visualization, including enabling Python in cells and referencing Excel data using the xl() function.

Start using Python in Excel: https://support.microsoft.com/en-US/Excel/python/get-started-with-python-in-excel… + Book ➔ Extending Excel with Python and R for advanced data manipulation and visualization: http://amzn.to/3V5lSLY — 𝒦𝑒𝓎 𝐹𝑒𝒶𝓉𝓊𝓇𝑒𝓈: Perform advanced data analysis and visualization techniques with R and Python on Excel data Use exploratory data analysis and pivot table analysis for deeper insights into your data Integrate R and Python code directly into Excel using VBA or API endpoints Purchase of the print or Kindle book includes a free PDF eBook
Original Article
View Cached Full Text

Cached at: 08/18/26, 04:38 PM

Start using Python in Excel: https://support.microsoft.com/en-US/Excel/python/get-started-with-python-in-excel… + Book ➔ Extending Excel with Python and R for advanced data manipulation and visualization: http://amzn.to/3V5lSLY — 𝒦𝑒𝓎 𝐹𝑒𝒶𝓉𝓊𝓇𝑒𝓈: Perform advanced data analysis and visualization techniques with R and Python on Excel data Use exploratory data analysis and pivot table analysis for deeper insights into your data Integrate R and Python code directly into Excel using VBA or API endpoints Purchase of the print or Kindle book includes a free PDF eBook


Get started with Python in Excel

Source: https://support.microsoft.com/en-US/Excel/python/get-started-with-python-in-excel For availability information, seePython in Excel availability. If you encounter any concerns with Python in Excel, please report them by selectingHelp>Feedbackin Excel.

New to Python in Excel? Start withIntroduction to Python in Excel.

Start using Python

To begin using Python in Excel, select a cell and on theFormulastab, selectInsert Python. This tells Excel that you want to write a Python formula in the selected cell.

Go to Formulas and then select Insert Python.

Or use the function**=PYin a cell to enable Python. After entering=PYin the cell, choose PY from the function AutoComplete menu with the Down arrow and Tab keys, or add an opening parenthesis to the function:=PY(**. Now, you can enter Python code directly into the cell. The following screenshot shows the AutoComplete menu with the PY function selected.

Enter =PY in a cell to enable Python.

Once Python is enabled in a cell, that cell displays aPYicon. The formula bar displays the same PY icon when the Python cell is selected. See the following screenshot for an example.

An Excel workbook with Python in Excel enabled in a cell, showing the green PY icon in the cell.

Combine Python with Excel cells and ranges

To reference Excel objects in a Python cell, make sure the Python cell is in Edit mode, and then select the cell or range that you want to include in the Python formula. This automatically populates the Python cell with the address of the cell or range that you selected.

Tip

Use the keyboard shortcutF2to toggle between Enter mode and Edit mode in Python cells. Toggling to Edit mode allows you edit the Python formula, and toggling to Enter mode allows you to select additional cells or ranges with your keyboard. To learn more keyboard shortcuts, seePython in Excel keyboard shortcuts.

Python in Excel uses the custom Python functionxl\(\)to interface between Excel and Python. Thexl\(\)function accepts Excel objects like ranges, tables, queries, and names.

You can also directly type references into a Python cell with thexl\(\)function. For example, to reference cellA1usexl\("A1"\)and for the rangeB1:C4usexl\("B1:C4"\). For a table with headers namedMyTable, usexl\("MyTable\[\#All\]", headers=True\). The**[#All]specifier ensures that the entire table is analyzed in the Python formula, andheaders=Trueensures that the table headers are processed correctly. To learn more about specifiers like[#All]**, seeUsing structured references with Excel tables.

The following image shows a Python in Excel calculation adding the values of cellA1andB1, with the Python result returned in cellC1.

Use the custom Python function xl() to interface between Excel and Python.

Formula bar

Use the formula bar for code-like editing behavior, like using the Enter key to create new lines. Expand the formula bar using the down arrow icon to view multiple lines of code at once. You can also use the keyboard shortcutCtrl+Shift+Uto expand the formula bar. The following screenshots show a formula bar before and after expanding it to view multiple lines of Python code.

Before expanding the formula bar: …

After expanding the formula bar:

Expand the formula bar to see more lines of code.

Output types

Use the Python output menu in the formula bar to control how Python calculations are returned. Return calculations as Python objects or convert calculations to Excel values and output them directly to a cell. The following screenshot shows the Python formula returned as an Excel value.

Tip

You can also use the right-click menu to change the Python output type. Open the right-click menu and go to Python Output, and then select the desired output type.

Use the Python output menu to switch between output types.

The following screenshot shows the same Python formula as the preceding screenshot, now returned as a Python object. When a formula is returned as a Python object, the cell displays a card icon.

Note

Formula results returned to Excel values are translated to their closest Excel equivalent. If you plan to reuse the result in a future Python calculation, it’s recommended to return the result as a Python object. Returning a result as Excel values allows you to run Excel analytics, such as Excel charts, formulas, and conditional formatting, on the value.

When data is returned as a Python object, the cell displays a Python card icon.

A Python object contains additional information within the cell. To view the additional information, open the card by selecting the card icon. The information displayed on the card is a preview of the object, which is useful when processing large objects.

Python in Excel can return many types of data as Python objects. A useful Python in Excel data type is a DataFrame object. To learn more about Python DataFrames, seePython in Excel DataFrames.

Import external data

All the data you process with Python in Excel must come from your worksheet or through Power Query. To import external data, use the Get & Transform feature in Excel to accessPower Query. For more information, seeUse Power Query to import data for Python in Excel.

Important

To protect your security, common external data functions in Python, such aspandas.read_csvandpandas.read_excel, aren’t compatible with Python in Excel. To learn more, seeData security and Python in Excel.

Calculation order

Traditional Python statements calculate from top to bottom. Within a Python in Excel cell, Python statements do the same thing—they calculate from top to bottom. But in a Python in Excel worksheet, Python cells calculate in row-major order. The cell calculations run across a row (from columnAto columnXFD), and then across each following row down the worksheet.

Python statements are ordered, so each Python statement has an implicit dependency on the Python statement that immediately precedes it in the calculation order.

The calculation order is important when defining and referencing variables in a worksheet, because you must define variables before you can reference them.

Important

The row-major calculation order also applies across worksheets within a workbook and is based on the order of the worksheets within the workbook. If you use multiple worksheets to analyze data with Python in Excel, make sure to include data and any variables storing data in cells and worksheets preceding the cells and worksheets that analyze that data.

Recalculation

When a dependent value of a Python cell changes, all Python formulas are recalculated sequentially. To suspend Python recalculations and improve performance, use eitherPartial CalculationorManual Calculationmode. These modes allow you to trigger a calculation when you’re ready. To change this setting, go to the ribbon and selectFormulas, then openCalculation Options. Next, select the desired calculation mode. ThePartial CalculationandManual Calculationmodes suspend automatic recalculation for both Python and Data Tables.

Disabling automatic recalculation in a workbook during Python development can improve performance and individual Python cell calculation speeds. However, you must manually recalculate the workbook to ensure accuracy in each Python cell. There are three ways to manually recalculate a workbook inPartial CalculationorManual Calculationmode.

  1. Use the keyboard shortcutF9.
  2. Go toFormulas>Calculate Nowin the ribbon.
  3. Go to a cell with a stale value, displayed with strikethrough formatting, and select the error symbol next to that cell. Then selectCalculate Nowfrom the menu.

Errors

Python in Excel calculations can return errors such as**#PYTHON!,#BUSY!, and#CONNECT!**to Python cells. To learn more, seeTroubleshoot Python in Excel errors.

Related articles

Similar Articles