Skip to content

Sorting spreadsheets based on one or multiple columns – quick solution for slow loading sheets in excel or google sheets

– simple code win in Python GIS tutorial

Use-case:

Sort a tabular data based on one or multiple columns using a simple and accessible python method. Specially useful for less technical background who may deal with a spreadsheet in excel or google sheets, which for any reason is loading slow. For larger data, it is not common to see there are issues in using the sort or filter functionality of excel or google sheet to directly sort the data there. Often leading to frozen sheets or slow loading of sheet.

We use a step-by-step guide here in how to use a code solution, download it, customize it based on your own need and save the sorted data back in its place.

https://youtu.be/qccaiFxptGA

You can also view the video tutorial of this guide in here:

In this example, I have a initial data in google sheets called MOCK_DATA (2) such as this

I want to sort this data based on last name alphabetically, or based on last name and then first name (can be extended to multiple other columns as well) and save the final sorted data back in google sheet.

This is how we proceed:

Step1:

initial setting and connect your google sheet and worksheet with their own name from your google sheet to google colab where the code exist.

Initial Settings

We add the necessary packages here and also connect the python code to google sheets where the sample data exists there.

Step2:

sort the sheet based on one column in python: column last name and with ascending order, meaning alphabetically here since it is a non-numerical or string type column.

Or you can sort sheet based on two columns: first last name, if they have the same last name, then sort based on first name like in this case:

Now, you may want to slice or filter your data to grab a smaller portion of data and save it. For slicing or filtering data based on specific condition on column and row values of data, you can refer to this tutorial and customize it for your need and combine it with sorting in this case if you want..

Lastly, save the sorted data back to google sheets:

the new sheet is added to the original google sheet as you can see.

Related link

Leave a Reply

Your email address will not be published. Required fields are marked *