A Multi-index conundrum

A Pandas series can have more than one index value. I discovered this fact during the week when I found the .groupby() method which was going to be very useful to me in getting a count of values.

I started with data similar to this and my objective was to get a summary of numbers of cars by colour:

This code produced the desired result (sort-of) except that I ended up with a Multi-index series which was not an animal I had yet encountered :).

summary = df.groupby(by = ["Cars", "Colours"])["Names"].count()

The help documentation on Multi-indexing provides a plethora of ways to deal with this, and I’m sure I’ll get there in time – but I kinda just wanted to get it back to a dataframe and move on with my task using the knowledge I had already acquired!

And this is where the .reset_index() method proved to be worth its weight in gold:

summary.reset_index(level=["Cars", "Colours"])

And voila!

How it works – Pandas, groupby method

Comments are closed.

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: