This Python Library Automates Pandas and Helps You Replace Excel | by Frank Andrade | Towards Data Science

Pandas is the best tool to do data analysis in Python and it has many advantages over tools like Microsoft Excel, but the transition between Excel to Python is challenging for those with little coding experience or that are new to Pandas.
Fortunately, that’s no longer the case. A few weeks ago I came across a Python library that helps us work with Pandas as if we were using Excel.
In this article, I will show you how to do things like creating pivot tables, joining tables, filtering data, and more using a Python library that allows us to work with a Pandas dataframe using an interface similar to Excel and automatically generates Pandas code for us.
Moving from Excel to Python has never been easier!
In this guide, the data we’ll use is a “StudentsPerformance” CSV file available on Google Drive, which I generated myself. This is random data that contains marks obtained by students in different subjects and, with the help of Mito, we will easily move from Excel spreadsheets to Python’s dataframes.
Make sure that the CSV file and your Python script are located in the same directory.
To easily create pivot tables, visualizations, and perform operations you would do in Excel or Pandas, we only need to install the mitosheet library (make sure you have Python 3.6 or above and also JupyterLab)
To install mitosheet, first, open a new terminal or command prompt and then run the following command (if possible, install it in a new virtual environment):
python -m pip install mitoinstallerpython -m mitoinstaller install
That’s everything we need to start working with Mito! For more details about the installation, check the official documentation.
To create a dataframe using Python we would need to import pandas and use the .read_csv() method, but with Mito, we only need to import mitosheet and initiate it by typing mitosheet.sheet().
import mitosheetmitosheet.sheet()
The code above will create a mitosheet. If the sheet does not render when you call mitosheet.sheet(), relaunch JupyterLab.
After this, we can import a dataset by clicking on the + button in the bottom left corner. Then, select the StudentsPerformance.csv file as shown below.
Once the CSV is imported, a dataframe is created using the same file name. In addition to that, Mito automatically generates the code in the cell below.
from mitosheet import *; register_analysis('UUID-b580825e-1ba5-47fc-851a-612cf52b9543')# Imported StudentsPerformance.csvimport pandas as pdStudentsPerformance_csv = pd.read_csv(r'StudentsPerformance.csv')
That’s everything you need to import data with mitosheet. Now let’s automate some common Pandas methods. You can continue reading or watch my video tutorial instead.
Adding a new column is a common task we do in Excel/Pandas. With Excel, we only need to right-click to create a new column, and with Pandas, we have to use the .insert() method.
With Mito, we can have the best of both worlds. To add a new column, you only need to click on the “Add Col” button. Then double-click on the column name to set the name you want.
Let’s do this by creating a new column that we will name “average.”
Once you’re done, check the cell below. Mito automatically generates the code that adds and renames a column.
Mito can help us sum row values as if we were using Microsoft Excel. We only need to locate the cell where we want to introduce the formula, then press = and select the columns we want to sum and write the + operator between them.
To show you better how this works, let’s calculate the average score of the math, reading, and writing exams. Locate a cell in the “average” column that we created in the previous step, write the formula shown in the gif below, press enter, and voilá!
In case you’re curious to know what Mito did, here’s a snippet of the code automatically generated by Mito.
We can filter data based on 1 or more conditions.
Let’s say we only want to show data related to the female gender. With Mito, we only need to select the “gender” column, then click on the funnel icon and choose what we want to filter. In this case, we go to the “filter” section, click on “add filter” and set “is exactly” equal to “female”
Now let’s say we want to show data related to the female gender that belongs to the group B.
Here we only need to repeat the steps we followed to select the female gender, but now with the group B. Select the “group” column, click on the funnel icon, go to the “filter” section, click on “add filter” and set “is exactly” equal to “group B”
The code below corresponds to the previous filters.
We can create a pivot table in Python as we would do in Excel using mitosheet. First, click on the “Pivot” button and then select the elements you wish to include in the rows, columns, and values.
In our example, we will create a pivot table that shows the mean of the math and reading scores of all the groups inside the race/ethnicity column (group column). To do so, follow the steps shown in the gif below.
In case you’re curious to know the code behind the curtains, here’s a snippet.
Just like Excel, Mito helps us create basic visualization with a couple of clicks in Python.
Let’s create a barplot of the pivot table we created before. To do so, click on the “Graph” button, then make sure the data source is the new pivot table “df2” we created and the chart type is set to “bar”
In the X-axis, we should include the “race/ethnicity” column (group column), and in the Y-axis, the “match score mean” column.
That’s it! We could do all of this in a couple of minutes with Mito. If you have a lot of experience with Pandas, probably you could get results as fast as Mito, but this library comes in handy, if you’re new to Pandas or Python
Merging 2 tables can be easily accomplished with Mito. In this example, we’ll see how to do an inner join. To do so, we need two tables with a column in common, so download the “StudentsPerformance_id” and “LanguageScore” CSV files from my Github. To import this second table, follow the steps shown in the first section of this article “Create a dataframe”
Once you have the two tables imported in Mito, click on the “Merge” button to join them. Make sure the “Merge Key” option is set to “id” in both tables (this is the column in common that we need to make an inner join)
After this, you will see there’s a new dataframe named “df3.” This is the result after joining the “StudentsPerformance_id_csv” and “LanguageScore_csv” dataframes.
Join my email list with 6k+ people to get my Python for Data Science Cheat Sheet I use in all my tutorials (Free PDF)
If you enjoy reading stories like these and want to support me as a writer, consider signing up to become a Medium member. It’s $5 a month, giving you unlimited access to thousands of Python guides and Data science articles. If you sign up using my link, I’ll earn a small commission with no extra cost to you.