Skip to content
Advertisement

How to unpack a Series of tuples in Pandas?

Sometimes I end up with a series of tuples/lists when using Pandas. This is common when, for example, doing a group-by and passing a function that has multiple return values:

JavaScript

What is the correct way to “unpack” this structure so that I get a DataFrame with two columns?

A related question is how I can unpack either this structure or the resulting dataframe into two Series/array objects. This almost works:

JavaScript

but it t is

JavaScript

and one needs to take the extra step of squeezing it.

Advertisement

Answer

maybe this is most strightforward (most pythonic i guess):

JavaScript

if you would want to rename the columns to something more meaningful, than:

JavaScript

if you do not want the default name for the index:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement