How to Use Python in Excel (Even If You've Never Written Code)
🔄 Life & Business How-To

How to Use Python in Excel (Even If You've Never Written Code)

Microsoft now lets you run Python directly inside your spreadsheet. Here's how to start, with five practical examples you can try today.

✦ This article was written by AI. It passed automated fact and quality checks; no human editor reviewed it.

You open a spreadsheet, scroll through a few thousand rows of orders, and try to spot which products sell best. The filter button gets you partway. A pivot table (a built-in summary tool that counts and totals your data) helps more. But the moment you think "I wish I could just describe what I want in plain English and have it happen" — that's where Python in Excel fits in.

Microsoft added Python directly to Excel. You type =PY in any cell, write a few short lines of Python, and the answer shows up right there in the grid. Nothing installs locally. Nothing new to learn.

What you need before you start

A few things should be in place so you don't hit a wall at step one.

  • A current Microsoft 365 subscription. Python in Excel is part of the Microsoft 365 family. Check the latest Microsoft 365 plan list to confirm your plan includes it — features are sometimes added or moved between tiers.
  • Excel on a desktop or the web. The richest support is in Excel for Windows, with Excel for Mac and Excel on the web also supported. Mobile support is more limited, so use a laptop for your first try.
  • An internet connection. Your Python code runs on Microsoft's servers, so offline workbooks can't compute Python cells.
  • A willingness to type a few non-English words. That's it. You don't need to know Python. You just need to copy the lines below, change one or two names, and press Enter.

What is Python, anyway?

Python is a programming language — a way to write instructions for a computer using words that look close to English. It's popular because the instructions are short and read almost like sentences.

You don't need to learn Python to use Python in Excel. Think of it like copy-pasting a formula, except this formula can do things a regular Excel formula can't — pull the average from only the rows that match a condition, or draw a chart, or reshape messy text in one step.

When you type =PY in a cell, Excel sends your Python code to Microsoft's cloud. The cloud runs it, returns the answer, and Excel displays the result — a number, a small table, or even a chart image. This takes a few seconds.

Step 1 — Pull data from a cell range into Python

Before Python can do anything useful, it needs to "see" your data. The trick is a function called xl() — think of it as Python pointing at a cell range and saying "that one."

  1. Click an empty cell away from your data — say cell H1.
  2. Type this exactly:
    =PY("df = xl('A1:D50')")
    
  3. Press Ctrl+Enter on Windows, or Cmd+Enter on Mac. This shortcut is what tells Excel "this is Python," not a regular formula.
  4. A small Python icon appears next to the cell, and the cell shows a table preview like [DataFrame].

💬 Example: =PY("df = xl('A1:D50')")

You'll know it worked when: the cell shows a tiny table icon or a small preview, not an error like #NAME? or #VALUE!. If you see an error, double-check that xl is lowercase and the cell range matches where your data actually lives.

Step 2 — Filter rows by a condition

Suppose you have a list of customer orders with columns named something like Date, Product, and Amount. You want to see only the rows where the Amount is greater than 1,000. In regular Excel, that's a filter. In Python, it's one short line.

  1. In a new empty cell, type:
    =PY("xl('A1:D500')[xl('A1:D500')['Amount'] > 1000]")
    
  2. Press Ctrl+Enter to commit. The square-bracket condition works like an Excel filter — it keeps only the rows where Amount is greater than 1,000.
  3. The cell now shows a smaller table containing only the big orders.

💬 Example: =PY("xl('A1:D500')[xl('A1:D500')['Amount'] > 1000]")

You'll know it worked when: the cell returns a smaller table than your original data. The label "Amount" must match your real column header — change it if your column is called "Total" or "Price."

Step 3 — Summarize a column with one line

A pivot table can do this too, but Python lets you write the rule in plain words. Here, you want the average of column "Amount" for each unique "Product."

  1. In a new cell, type:
    =PY("xl('A1:D500').groupby('Product')['Amount'].mean()")
    
  2. Press Ctrl+Enter.
  3. The result is a small two-column table: each unique product on the left, the average amount on the right.

💬 Example: =PY("xl('A1:D500').groupby('Product')['Amount'].mean()")

You'll know it worked when: you see a table where each row is one product with a number beside it. If your column has a different name, swap 'Product' and 'Amount' for your actual headers. Use .sum() instead of .mean() if you want totals.

Step 4 — Draw a chart inside a cell

This is the part that feels a little like magic. You ask matplotlib (a Python library for drawing charts) to draw a bar chart, and Excel shows the chart right inside the spreadsheet, in a single cell.

  1. In a new cell, type:
    =PY("import matplotlib.pyplot as plt; xl('A1:D500').groupby('Product')['Amount'].sum().plot(kind='bar'); plt.show()")
    
  2. Press Ctrl+Enter.
  3. Wait a few seconds. Excel shows a chart image inside the cell.

💬 Example: =PY("import matplotlib.pyplot as plt; xl('A1:D500').groupby('Product')['Amount'].sum().plot(kind='bar'); plt.show()")

You'll know it worked when: the cell shows a small bar chart, not text. If the chart looks squashed, drag the row taller or widen the column — the chart resizes with the cell.

Step 5 — Save and reopen your workbook

Python in Excel doesn't change how you save a file. Your workbook stays a normal .xlsx, and each Python cell stores both the code and the last computed result.

  1. Press Ctrl+S (Windows) or Cmd+S (Mac), or go to File → Save.
  2. Keep the default .xlsx format — that's fine for Python in Excel workbooks.
  3. When you reopen the file later, Excel will show the saved results immediately. If you edit a cell the Python code depends on, the Python cell will refresh the next time you click it.

💬 Example: nothing to copy here — just File → Save.

You'll know it worked when: the file opens normally and your Python cells still display their values. Anyone you share the file with sees the results too — they don't need Python installed to view it.

Common mistakes

  • Forgetting Ctrl+Enter. Pressing just Enter treats =PY(...) as a regular formula and gives you a #NAME? error. The shortcut is what tells Excel "this is Python."
  • Typos in column names. ['Amount'] only works if your column is literally named "Amount." Check the header row of your data and match the spelling exactly — including capitalization.
  • Cell range too small or too large. If your data runs to row 800 but you wrote A1:D50, Python only sees the first 50 rows.
  • Working offline. Python runs in Microsoft's cloud, so a workbook opened without internet won't compute new Python results.
  • Trying it on a phone first. Mobile Excel has limited Python support. Use a laptop or the web version for your first attempt.

A first next step to try today

Open a spreadsheet you've been meaning to clean up. Find an empty cell and try Step 1 first — just pulling your data into Python. Once you see that work, try Step 3 with your own column names. The feeling of typing a sentence and having the computer obey it is the same one every programmer had on day one.

You don't need to become a Python expert. You just need one line that does something useful for you.

Keep reading

Was this helpful?

✦ Generated by AI in AI World HQ's automated newsroom, from official sources. Checked by automated fact and quality gates — no human editor reviewed this article. Spot a mistake? Use the buttons above.

☕ Free to read, no ads, no paywall — readers keep it going. Support us (one-off or monthly)

← Back to all stories