Dataframe.unstack() in Pandas

In this tutorial, we will learn how to use the method unstack() in pandas. This method is mainly used to reshape the data frame or series. The data frame consists of columns with inner levels consisting of indexes. The final data frame has the results in sorted order.  If there is only one index, the result will be a series. To understand this, let’s begin the tutorial.

Parameters of Dataframe.unstack() in Pandas

The following are the parameters of this method:

level: The default value is -1. Users can provide the level in the form of a string, integer or a list of them. It is used to specify the level to be unstacked.

fill_value: This is used to replace Nan in the data frame if the result contains them.

Data frame

Create a data frame with the following data.

import pandas as p
r=p.MultiIndex.from_tuples([('y','z'),('y', 'x'),('s','t'),('s','u')])
t=p.DataFrame([[3,4,5,6],[7,8,9,10],[11,12,13,14],[15,16,17,18]],index=r)
print(t)

OUTPUT:

    0  1  2  3
y z 3  4  5  6
  x 7  8  9  10
s t 11 12 13 14
  u 15 16 17 18

Example 1 of unstack()

Here we will use the unstack() method on the data frame without any parameters.

import pandas as p
r=p.MultiIndex.from_tuples([('y','z'),('y', 'x'),('s','t'),('s','u')])
t=p.DataFrame([[3,4,5,6],[7,8,9,10],[11,12,13,14],[15,16,17,18]],index=r)
print(t.unstack())

OUTPUT:

Using the unstack() method results in the data frame consisting of 2 rows and 16 columns.

  0                 1             ... 2            3 
  t    u    x   z   t    u    x   ... u    x   z   t    u    x    z
s 11.0 15.0 NaN NaN 12.0 16.0 NaN ... 17.0 NaN NaN 14.0 18.0 NaN  NaN
y NaN  NaN  7.0 3.0 NaN  NaN  8.0 ... NaN  9.0 5.0 NaN  NaN  10.0 6.0

Example 2

Here we will use the unstack() method with level parameter.

import pandas as p
r=p.MultiIndex.from_tuples([('y','z'),('y', 'x'),('s','t'),('s','u')])
t=p.DataFrame([[3,4,5,6],[7,8,9,10],[11,12,13,14],[15,16,17,18]],index=r)
print(t.unstack(level=0))

OUTPUT:

Here we unstack level 0.

  0        1        2        3 
  s    y   s    y   s    y   s    y
t 11.0 NaN 12.0 NaN 13.0 NaN 14.0 NaN
u 15.0 NaN 16.0 NaN 17.0 NaN 18.0 NaN
x NaN  7.0 NaN  8.0 NaN  9.0 NaN  10.0
z NaN  3.0 NaN  4.0 NaN  5.0 NaN  6.0

Example 3: unstack() with fill_value

Here we will use the unstack() method with fill_value parameter.

import pandas as p
r=p.MultiIndex.from_tuples([('y','z'),('y', 'x'),('s','t'),('s','u')])
t=p.DataFrame([[3,4,5,6],[7,8,9,10],[11,12,13,14],[15,16,17,18]],index=r)
print(t.unstack(level=0,fill_value=25))

OUTPUT:

Here we will fill all the Nan values with 25.

  0     1     2     3 
  s  y  s  y  s  y  s  y
t 11 25 12 25 13 25 14 25
u 15 25 16 25 17 25 18 25
x 25 7  25 8  25 9  25 10
z 25 3  25 4  25 5  25 6

Also read: Understanding Python pandas.DataFrame.boxplot

Leave a Reply

Your email address will not be published.