Pandas Tutorial - Stack(), Unstack() and Melt() - MLK - Machine Learning Knowledge (2024)

  • Palash Sharma
  • Last Updated On August 1, 2021
  • Python Pandas

Table of Contents

In our machine learning and data science projects e often have to explore pandas dataframes in different ways for extracting more information. For this purpose we will learn about pandas stack(), unstack() and melt() in this tutorial The functions will be explained with the help of syntax and examples. So let us begin the article.

Importing Pandas Library

Initially, we will load the Pandas library.

In[1]:

import pandas as pdimport numpy as np

Pandas Stack : stack()

The stack function of pandas is used for stacking the levels from columns to index.

Syntax

pandas.DataFrame.stack(level,dropna)

level : int, str, list, default – Here the levels from where columns are stacked are provided.

dropna : bool, default True – This parameter is used to decide whether null values should be dropped or not.

Example 1: Using Pandas Stack on single level column

In this example, we will be applying stack() function on single level column. We have created a dataframe and then the pandas stack() function is applied over the column.

In[2]:

df_single_level_cols = pd.DataFrame([[7, 9], [11, 13]], index=['Bugatti', 'Mclaren'], columns=['weight', 'speed'])

In[3]:

df_single_level_cols

Out[3]:

weightspeed
Bugatti79
Mclaren1113

In[4]:

df_single_level_cols.stack()

Out[4]:

Bugatti weight 7 speed 9Mclaren weight 11 speed 13dtype: int64

Example 2: Using multi-level columns with pandas stack()

Here multindex dataframe is created and then the stack function is applied.

In[5]:

multicol1 = pd.MultiIndex.from_tuples([('speed', 'kmh'), ('speed', 'mph')])

In[6]:

df_multi_level_cols1 = pd.DataFrame([[7, 9], [18, 25]], index=['Aston Martin', 'Bentley'], columns=multicol1)

In[7]:

df_multi_level_cols1

Out[7]:

speed
kmhmph
Aston Martin79
Bentley1825

In[8]:

df_multi_level_cols1.stack()

Out[8]:

speed
Aston Martinkmh7
mph9
Bentleykmh18
mph25

[adrotate banner=”3″]

Pandas Unstack : unstack()

The pandas unstack() function pivots a level of hierarchical index labels.

Syntax

pandas.DataFrame.unstack(level,fill_value)

level : int, str, or list of these, default -1 (last level) – Here the levels of index which are to be unstacked are passed in this parameter.

fill_value : int, str or dict : Using this parameter, we can fill the null values usin this parameter.

Example 1: Using series data with pandas unstack function

In this example, series data is used for applying the unstacking operation.

In[9]:

index = pd.MultiIndex.from_tuples([('red', 'p'), ('red', 'q'), ('blue', 'p'), ('blue', 'q')])

In[10]:

s = pd.Series(np.arange(6.0,10.0), index=index)

The unstack function has a parameter known as level. We have used this parameter for performing the unstack operation.

In[11]:

s.unstack(level=-1)

Out[11]:

pq
blue8.09.0
red6.07.0

In[12]:

s.unstack(level=0)

Out[12]:

bluered
p8.06.0
q9.07.0

Example 2: Applying unstack() function on dataframe

In this example, pandas unstack() function is applied to dataframe data by using level parameter.

In[13]:

df = s.unstack(level=0)

In[14]:

df.unstack()

Out[14]:

blue p 8.0 q 9.0red p 6.0 q 7.0dtype: float64

Pandas Melt : melt()

Pandas melt() function is used for unpivoting a DataFrame from wide to long format.

Syntax

pandas.DataFrame.melt(id_vars=None, value_vars=None, var_name=None, value_name=’value’, col_level=None)

id_vars : tuple, list, or ndarray, optional – Here the columns are passed that will be used as identifier values.

value_vars : tuple, list, or ndarray, optional – In this parameter, columns that we want to melt are selected.

var_name : scalar – This is the name used for the variable column.

value_name : scalar,default value – This is the name used for value column

col_level : int or str, optional – If columns are a MultiIndex then use this level to melt.

Example 1: Using single level dataframes for pandas melt() function

In this example, the melt function of pandas is applied on single level dataframes.

In[15]:

df = pd.DataFrame({'A': {0: 'p', 1: 'q', 2: 'r'}, 'B': {0: 2, 1: 4, 2: 6}, 'C': {0: 5, 1: 7, 2: 9}})

In[16]:

df

Out[16]:

ABC
0p25
1q47
2r69

In the examples discussed below, we are changing the existing shape of dataframe with the help of pandas melt function.

In[17]:

pd.melt(df, id_vars=['A'], value_vars=['B'])

Out[17]:

Avariablevalue
0pB2
1qB4
2rB6

In[18]:

pd.melt(df, id_vars=['A'], value_vars=['B', 'C'])

Out[18]:

Avariablevalue
0pB2
1qB4
2rB6
3pC5
4qC7
5rC9

Example 2: Using multi-level dataframes for pandas melt function

Using pandas melt() function, we are using multi-level dataframes in this example.

In[19]:

df.columns = [list('PQR'), list('XYZ')]

In[20]:

df

Out[20]:

PQR
XYZ
0p25
1q47
2r69

In[21]:

pd.melt(df, col_level=0, id_vars=['P'], value_vars=['Q'])

Out[21]:

Pvariablevalue
0pQ2
1qQ4
2rQ6

In[22]:

pd.melt(df, id_vars=[('P', 'X')], value_vars=[('Q', 'Y')])

Out[22]:

(P, X)variable_0variable_1value
0pQY2
1qQY4
2rQY6

Conclusion

We have reached the end of this article, in this article we learned about pandas functions that can help in changing the shape of dataframe. We covered pandas functions of stack(), unstack() and melt()along with syntax and examples.

  • Also Read –Tutorial – Pandas Drop, Pandas Dropna, Pandas Drop Duplicate
  • Also Read –Pandas Visualization Tutorial – Bar Plot, Histogram, Scatter Plot, Pie Chart
  • Also Read –Tutorial – Pandas Concat, Pandas Append, Pandas Merge, Pandas Join
  • Also Read –Pandas DataFrame Tutorial – Selecting Rows by Value, Iterrows and DataReader

Reference –https://pandas.pydata.org/docs/

  • Pandas Tutorial - Stack(), Unstack() and Melt() - MLK - Machine Learning Knowledge (1)

    Palash Sharma

    I am Palash Sharma, an undergraduate student who loves to explore and garner in-depth knowledge in the fields like Artificial Intelligence and Machine Learning. I am captivated by the wonders these fields have produced with their novel implementations. With this, I have a desire to share my knowledge with others in all my capacity.

    View all posts

  • Tags:machine learning, pandas, python

Follow Us

Related Posts

7 Ways to Drop Column in Pandas DataFrame

Pandas iterrows() Method Explained with Examples

Pandas DataFrame Query() Method Explained with Example

Pandas Cut Function Tutorial | pd.cut() Explained with Examples

Pandas DataFrame Copy Function with Examples

Pandas Tutorial - Stack(), Unstack() and Melt() - MLK - Machine Learning Knowledge (2024)
Top Articles
Latest Posts
Article information

Author: Fr. Dewey Fisher

Last Updated:

Views: 5943

Rating: 4.1 / 5 (62 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Fr. Dewey Fisher

Birthday: 1993-03-26

Address: 917 Hyun Views, Rogahnmouth, KY 91013-8827

Phone: +5938540192553

Job: Administration Developer

Hobby: Embroidery, Horseback riding, Juggling, Urban exploration, Skiing, Cycling, Handball

Introduction: My name is Fr. Dewey Fisher, I am a powerful, open, faithful, combative, spotless, faithful, fair person who loves writing and wants to share my knowledge and understanding with you.