What are stack and unstack functions in the Python Pandas library.


Stack and unstack functions are used to reshape a DateFrame in the pandas library to extract more information in different ways.

Stack

Pandas stack is used for stacking the levels from column to index. It returns a new DataFrame or Series with a multi-level index. The stack method has 2 parameters which are level and dropna.

The level parameter is used to stack from the column axis onto the index axis, the default value is 1, and we can give string, list, and integer. As well as dropna is used to remove rows in the resultant DataFrame/Series with missing values. By default it is True, and it takes Boolean data.

Let’s take an example and see how it will work.

Example

df = pd.read_json('E:\iris.json') # create a DataFrame using iris data set
df_stacked = df.stack(0) # stacked to level 0
print(df_stacked) # display the result

Explanation

Using the iris data set, we have created a DataFrame and it has only one level which is 0. In the next step, stacked that DataFrame df into index level.

Output

0   sepalLength          5.1
    sepalWidth           3.5
    petalLength          1.4
    petalWidth           0.2
    species           setosa
...
149   sepalLength        5.9
      sepalWidth           3
      petalLength        5.1
      petalWidth         1.8
      species      virginica
Length: 750, dtype: object

The stacked DataFrame “df” is shown in the above output block, the resultant output is a Series with 2 levels which are 0 and 1, and the Series length is 750.

Unstack

Unstack is also similar to the stack method, it returns a DataFrame having a new level of column labels. It has 2 parameters which are level and fill_value.

The level parameter takes an integer, string, list of these, and the Default value is 1 (1 is the last level). And fill_value is used to fill null values if unstack produces any missing value.

Example

In this example, we will take the above stacked DataFrame and apply it to the unstack function.

unstacked_df = df_stacked.unstack(level=0)
print(unstacked_df)

Explanation

Unstacked the stacked DataFrame from the above example. Here the level is 0. And the resultant output DataFrame is displayed in the below block.

Output

              0         1         2        3          4        5         6         7     \
sepalLength    5.1       4.9       4.7       4.6         5       5.4       4.6         5
sepalWidth     3.5         3       3.2       3.1       3.6       3.9       3.4       3.4
petalLength    1.4       1.4       1.3       1.5       1.4       1.7       1.4       1.5
petalWidth     0.2       0.2       0.2       0.2       0.2       0.4       0.3       0.2
species     setosa    setosa    setosa    setosa    setosa    setosa    setosa    setosa

               8           9  ...        140          141          142          143 \
sepalLength    4.4       4.9  ...        6.7          6.9          5.8          6.8
sepalWidth     2.9       3.1  ...        3.1          3.1          2.7          3.2
petalLength    1.4       1.5  ...        5.6          5.1          5.1          5.9
petalWidth     0.2       0.1  ...        2.4          2.3          1.9          2.3
species     setosa    setosa  ...  virginica    virginica    virginica    virginica

                 144          145          146          147          148          149
sepalLength      6.7          6.7          6.3          6.5          6.2          5.9
sepalWidth       3.3            3          2.5            3          3.4            3
petalLength      5.7          5.2            5          5.2          5.4          5.1
petalWidth       2.5          2.3          1.9            2          2.3          1.8
species    virginica    virginica    virginica    virginica    virginica    virginica

[5 rows x 150 columns]

The unstacked DataFrame with level 0 has 5 rows and 150 columns. Using these stack and unstack functions we can easily extract the data from DataFrame and we can easily reshape the multilevel DataFrames.

Updated on: 18-Nov-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements