How to Write Excel-like Formulas in Python | by Roman Orac | Towards Data Science

Many Python users are transitioning from spreadsheets because of a Python package that allows users to use Excel-like syntax. It is a spreadsheet environment for JupyterLab to help you with your Python analysis.
Meet Mito — a Python package that initializes an interactive spreadsheet into your JupyterLab Environment. For each edit you make in the spreadsheet, Mito generates production-ready Python code. It’s also free to use.
By reading this article you’ll learn:
How to perform basic Excel-like operations in Python
How to write Excel-like formulas in Python
In case you’ve missed my previous articles about this topic, see Mito — A Spreadsheet that Generates Python.
To use Excel syntax, first initialize the Mitosheet using the commands:
import mitosheetmitosheet.sheet()
This will render the interactive spreadsheet, where you can:
pass in a dataset from your local files
or you can pass in an existent DataFrame in your notebook as an argument to the Mitosheet call:
mitosheet.sheet(df))
To install Mito, run these commands:
python -m pip install mitoinstallerpython -m mitoinstaller install
Here are the full install instructions.
To add a column, click the “ADD COL” button in the toolbar at the top of the Mitosheet. With the new column, you can use spreadsheet functions and formulas.
When you add a column, it generates the equivalent Python in the code cell below the Mitosheet.
To rename a column just double click the column's title and type in whichever new title you want.
Within the cells of the new column, you can assign a formula.
In the new column, you can use normal Excel-like functions and Mito will generate the equivalent Python code.
As you begin to type your function, a suggestion list of possible functions to choose from will populate a menu below.
The function used in the image above generates the equivalent Python (which is auto-documented).
Airport_Pets_csv['New_Column'] = IF(Airport_Pets_csv['Division'] == "Central", 1, 0)
Mito has many different types of Excel functions ready to use in the sheet. You can use:
Date Manipulation
Statistical Analysis
Data Cleaning
and more.
See the formula reference page for all supported Excel-like formulas.
Mito allows the users to select any cell in the DataFrame and change a specific value — just as they would in a spreadsheet. This can be an important feature for quick data editing and data validation.
This edit generates the equivalent code:
# Set column City at index 0 in Airport_Pets_csv to AmherstAirport_Pets_csv.at[0, 'City'] = "Amherst"
Mito also gives users a familiar spreadsheet environment to complete other common Python tasks.
Users can create pivot tables in an interactive setting, and have the equivalent code these tables generated automatically:
Users can generate graphs and the equivalent code as well:
Mito also supports common analysis features such as:
filtering
sorting
summary statistics
merging dataset
macros
exporting to CSV
Mito is a powerful tool for those wishing to transition from spreadsheet environments like Excel or Google Spreadsheets to Python.
A neet Mito feature is that it shows how it calculates certain operations. So you can learn while clicking through your analysis.
As an experienced pandas user, I use Mito for the initial Exploratory Data Analysis. With Mito I avoid writing the same pandas commands over and over.
If you enjoy reading these stories, why not become a Medium paying member? It is $5 per month, and you will get unlimited access to 10000s of stories and writers. If you sign up using my link, I will earn a small commission.