Learn with Yasir

Share Your Feedback

Introduction to Python in Excel


Learn how to use Python in Microsoft Excel to analyze data, perform calculations, create charts, and automate data analysis using Python code.

Python in Excel allows you to write and execute Python code directly inside Microsoft Excel worksheets. You can use Python for data analysis, statistics, data visualization, and advanced calculations without leaving Excel.

It combines Excel’s spreadsheet capabilities with Python libraries such as pandas, NumPy, Matplotlib, and Seaborn.

1. How to enable Python in Excel

![How to Use Python in Excel Built In](https://images.openai.com/static-rsc-4/hbb9S_J5hJ9BvCo1J7u3WcJTrQMND-Re8ldoIDdFZAZzmZz5DsapgDNOtgrN_3s30f2JLt2Tn4EmuRFUSK6UO0rxbOnZm3XQhu1B6GUAhKYUUfGk9iuu2ytzY0_fTr3F5aZXcuzscP1wMgPzKn69Lq-Q7-gGfrIW5VZdeq5zZcI?purpose=inline)

Writing Python code within an Excel workbook to fill cells

PY function: How to use Python in Excel - gHacks Tech News

  1. Open Microsoft Excel.
  2. Open a supported Microsoft 365 Excel workbook.
  3. Select a cell.
  4. Go to Formulas → Insert Python or select Insert Python from the ribbon.
  5. Excel creates a Python cell.
  6. Enter your Python code.

You can also type:

=PY(

2. First Python example in Excel

Enter this in a Python cell:

print("Hello, Python in Excel!")

Another example:

import mathmath.sqrt(144)

Expected result: 12

3. Use Python to analyze Excel data

Suppose you have student marks in Excel:

Student Marks
Ali 85
Ahmed 72
Sara 92
Ayesha 78
Usman 65

You can use Python to calculate the average:

import pandas as pd
marks = xl("A2:B6", headers=True)
marks["Marks"].mean()

Calculate the highest marks:

marks["Marks"].max()

Calculate the lowest marks:

marks["Marks"].min()

4. Create charts using Python

For example, create a bar chart of student marks:

import matplotlib.pyplot as plt
plt.bar(marks["Student"], marks["Marks"])
plt.title("Student Performance")
plt.xlabel("Students")
plt.ylabel("Marks")plt.show()

Excel displays the resulting chart as a Python visual.

Microsoft Excel to let you run Python scripts as formulas

bleepingcomputer.com

5. What can you do with Python in Excel?

Application Python libraries
Data cleaning pandas, NumPy
Statistical analysis SciPy, statistics
Data visualization Matplotlib, Seaborn
Machine learning scikit-learn
Data exploration pandas
Predictive analysis scikit-learn, NumPy

6. Important differences

Python in Excel Traditional Python
Runs inside Excel Runs outside Excel
Uses =PY() Uses .py files
Runs in Microsoft Cloud Runs locally or on a server
Uses xl() to access Excel data Uses pandas and other libraries to read files
Requires internet Can work offline

Python in Excel has security restrictions. For example, you cannot directly use pandas.read_excel() to open arbitrary local files. Import external data through Power Query instead.

image

Microsoft Support+1

7. Check whether your Excel supports Python

Python in Excel requires an eligible Microsoft 365 subscription and supported Excel version. Availability differs between Business, Education, Personal and Family subscriptions.

image

8. Suggested lessons for your Microsoft Excel Mastery Series

I recommend adding a separate module:

Python in Excel: Learn Python for Data Analysis

  1. Introduction to Python in Excel
  2. Enabling Python in Excel
  3. Python Syntax and Basic Operations
  4. Working with Excel Data Using xl()
  5. Data Analysis Using Pandas
  6. Data Cleaning Using Python
  7. Statistical Analysis Using Python
  8. Data Visualization Using Matplotlib
  9. Data Analysis Using Seaborn
  10. Introduction to Machine Learning in Excel
  11. Practical Projects Using Python and Excel

Further Reading

References and Bibliography