Python For Excel Free Book
Python For Excel Free Book
Bb
If you want someone else to download this book, send them here:
https://chandoo.org/wp/python-for-excel-freebook
Thank you
Written by:
Contents
What is Python for Excel feature? .......................................................................................... 5
How do I enable Python for Excel? ........................................................................................ 5
I’ve joined the Insider Program; how do I know if I got Python? ................................................. 6
If you don’t see “Python” buttons in Formula ribbon:........................................................... 6
How to use Python in Excel – a quick tutorial ......................................................................... 6
10 Python Code Samples ..................................................................................................... 8
EXAMPLE 0...................................................................................................................... 8
EXAMPLE 1...................................................................................................................... 9
EXAMPLE 2...................................................................................................................... 9
EXAMPLE 3.....................................................................................................................10
EXAMPLE 4.....................................................................................................................10
EXAMPLE 5.....................................................................................................................11
EXAMPLE 6.....................................................................................................................11
EXAMPLE 7.....................................................................................................................12
EXAMPLE 8.....................................................................................................................13
EXAMPLE 9.....................................................................................................................14
EXAMPLE 10 ...................................................................................................................15
Errors and Problems: ..........................................................................................................15
Execution order of your code:...........................................................................................16
Resources to Learn Python..................................................................................................17
Python Videos ................................................................................................................17
Python Books .................................................................................................................18
Microsoft Resources ......................................................................................................18
You can now write Python code natively in Excel cells and return the output as either Python
objects or Excel values. For example, you want to perform quick statistical analysis of your sales
data in the range A1:D10. You can use the below Python code to do this now.
=XL("A1:D10", headers=True).describe()
If you have Excel 365, you can go to File > Account to enable “insider” program. More details on
eligibility and instructions are here - https://insider.microsoft365.com/en-us/join/windows
After you’ve joined the program, update your Office from File > Account page.
Beverie Moffet Fruit & Nut Bars India 14-Jul-23 6573 598
Ches Bonnell 99% Dark & Pure New Zealand 16-Aug-23 12901 993
Gunar Cockshoot Fruit & Nut Bars New Zealand 19-Jul-23 11935 1492
Karlen McCaffrey 99% Dark & Pure USA 6-Aug-23 2247 205
Marney O'Breen Spicy Special Slims New Zealand 19-Aug-23 735 147
1. Once you have some data in Excel, press CTRL ALT SHIFT P to enable Python mode. If
you get a “welcome to Python screen” complete the tour and then press the shortcut
again.
2. Using your mouse or keyboard, select the data in your workbook. Excel should write the
necessary XL() command to capture your data into Python as a dataframe.
DATAFRAME: a dataframe is a python concept for storing data. They are like
Excel tables. Each column of dataframe has one kind of data.
3. To see the dataframe you just built, press CTRL Enter. Excel will display a “Python
Object” in the cell.
To see the “actual” data instead of Python Object, right click on the cell and change output to
Excel values.
Before starting.
• Copy the above table of sample data and paste it in Excel (in range A1:F30).
Alternatively, download this file with the data.
• To type the code, enter python mode (CTRL ALT SHIFT P) or use the formula =PY( in a
cell.
EXAMPLE 0
Construct dataframe
df = xl("A1:F30", headers=True)
EXAMPLE 1
Description of the data
df.describe()
EXAMPLE 2
Description of the data, all columns
df.describe(include="all")
EXAMPLE 3
Unique Product Names
df["Product"].unique()
EXAMPLE 4
Add “Sales per Box” calculated column to the dataframe
df["sales per box"]=df["Sales"]/df["Boxes"]
Homework Problems
Can you add calculated columns for below situations:
EXAMPLE 5
Add “Sales as percentage” calculated column to the dataframe
total_sales = sum(df.Sales)
df["Sales as a percentage"] = df["Sales"]/total_sales
df
TIP: Do you notice the different ways in which you can refer dataframe columns? You can use
dot notation (ex: df.Sales) or bracket notation (ex: df[“Sales”])
EXAMPLE 6
Group Sales by Date and Show a Pivot
df.groupby(by="Date").sum()
EXAMPLE 7
Group Sales by Date but only show Sales & Boxes columns
df.groupby("Date")[["Sales", "Boxes"]].sum()
EXAMPLE 8
Create a bar graph with Daily Sales
import matplotlib.pyplot as plt
plt.bar(df["Date"], df["Sales"])
EXAMPLE 9
Create a bar graph with Daily Sales – another method
df_groups = df.groupby("Date")["Sales"].sum()
df_groups.plot(kind="bar")
EXAMPLE 10
Filter the dataframe to show all records where the product has the word “Choc”
df[df["Product"].str.contains("Choc")]
Homework Problems
• Can you filter all the records that have either “Choc” or “choc”?
• Create a bar graph of this data to show total sales by each product
• If there is an error in your code, Excel will show the error code #PYTHON! In the cell.
• Excel will also automatically open the diagnostics panel with more information about
your error.
1. See if you can enable use Python in Excel to get a feel of the technology.
2. Install a proper Python IDE like Anaconda, VS Code or something else to learn & practice
Python properly.
3. Understand the Python programming concepts like variables, conditions, list
comprehension, dataframes and EDA. Here is a good article on the process.
4. Apply these concepts on your own / business data to solidify your understanding.
5. If you need practice datasets, try Kaggle.
Python Videos 📺
Python in Excel (video by Chandoo)
[NEW]
Python Books 📚
Python Crash Course 2nd Edition by Eric Matthes - https://amzn.to/3PBzYRK
This is the book we all (Jo, kids & I) read and really loved it. The explanations and examples are
easy enough to get started. There is enough variety to please everyone.
More practical if you want to get things done with Python. I read it a few times and really like the
practicality of the book.
Python is particularly useful for doing data science & building machine learning models. This is
an area of focus for me in the next months. I suggest getting the Python Data Science book once
you have strong foundation in the language.
Microsoft Resources 🌐
As part of the Python for Excel launch, Microsoft also added many resources and example
pages to their website. Check out these pages.