Microsoft Excel has evolved far beyond basic spreadsheets. With the integration of Python in Excel, you can now harness the power of data science directly within your sheetsโno need to switch apps!
In this post, weโll demonstrate how to use Python in Excel to analyze data like:
Rank | City | State | Estimate | Census |
---|---|---|---|---|
1 | Mumbai | Maharashtra | 20667656 | 20411000 |
2 | Delhi | Delhi | 19814000 | 16787941 |
3 | Bengaluru | Karnataka | 12224000 | 8436675 |
4 | Hyderabad | Telangana | 9980000 | 6809970 |
Table of Contents
๐ What Youโll Learn
- How to enable Python in Excel
- Run basic Python code inside Excel
- Calculate growth, percentage changes, and filter top cities
โ Step 1: Enable Python in Excel
- Make sure you have Microsoft 365 (latest update) with Python in Excel Preview access.
- Click on any cell.
- Go to Formulas > Insert Python (or type
=PY()
).
๐ Step 2: Write Python Code to Analyze Data
Assume your Excel table is in range A1:E5.
A. Calculate Population Growth
=PY("
import pandas as pd
df = workbook.sheets[0].range('A1:E5').to_pandas()
df['Growth'] = df['Estimate'] - df['Census']
df
")
B. Calculate Growth Percentage
=PY("
df['Growth %'] = (df['Growth'] / df['Census']) * 100
df
")
๐ Step 3: Sort by Growth %
excelCopyEdit=PY("
df_sorted = df.sort_values('Growth %', ascending=False)
df_sorted
")
๐ Benefits of Python in Exce
- Run pandas, matplotlib, and other popular Python libraries.
- No need to install anythingโruns in a secure Microsoft environment.
- Combine Excel formulas + Python logic for advanced analysis.
๐ง Use Cases
- Census growth comparisons
- Predictive modeling on population
- Data visualization (with Python graphs in future updates)
๐ Final Thoughts
With Python in Excel, you bridge the gap between spreadsheet simplicity and coding power. Itโs a game-changer for analysts, researchers, and anyone who loves data!