Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pandas df.unstack() Function

Pandas df.unstack() Function

Pandas Common Functions Common Pandas functions


df.unstack() is a member method of DataFrame, used toConvert row indexes to column indexes. It "unstacks" data with a hierarchical index, moving one or more row levels into the columns.

This is an important data reshaping operation, often used together with stack() to achieve mutual conversion of data.

Word meaning: unstack means "to take apart and spread out"; here it refers to "unstacking" row indexes into columns, turning a long table into a wide one.


Basic syntax and parameters

df.unstack() is an instance method of DataFrame, called via the dot operator.

Syntax format



DataFrame.unstack(level=-1, fill_value=None)

Parameter Description

- Type: int, str, list of int, or list of str. - Description: Specifies the level(s) to unstack into columns. For a MultiIndex, you can specify a specific level number (starting from 0) or a level name. The default is -1, meaning the innermost level. - Type: scalar or None. - Description: The value used to fill missing values produced by the unstack operation. The default is None, which preserves NaN.

Function description


Example

Let us thoroughly master df.unstack() through a series of examples from simple to complex.

Example 1: Basic usage - converting rows to columns

Example

import

pandas

as

pd

1. Create long-format data

df

=

pd.

DataFrame

(

{

   

'product'

:

[

'A'

,

'A'

,

'B'

,

'B'

,

'C'

,

'C'

]

,

   

'region'

:

[

'North'

,

'South'

,

'North'

,

'South'

,

'North'

,

'South'

]

,

   

'sales'

:

[

100

,

150

,

200

,

180

,

220

,

250

]

}

)

print

(

"=== Original long-format data ==="

)

print

(

df

)

2. Set a hierarchical index

df_indexed

=

df.

set_index

(

[

'product'

,

'region'

]

)

print

(

"n=== After setting the hierarchical index ==="

)

print

(

df_indexed

)

3. Use df.unstack() to convert the inner index into columns

unstacked

=

df_indexed.

unstack

(

)

print

(

"n=== df.unstack() result ==="

)

print

(

unstacked

)

4. Simplify the view

print

(

"n=== Sales only ==="

)

print

(

unstacked

[

'sales'

]

)

Expected output:



=== 原始长格式数据 ===

  product region  sales

0       A  North    100

1       A  South    150

2       B  North    200

3       B  South    180

4       C  North    220

5       C  South    250

n=== 设置层次化索引后 ===

                sales

product region       

A       North     100

        South     150

B       North     200

        South     180

C       North     220

        South     250

n=== df.unstack() 结果 ===

        sales      

region  North South

product            

A         100   150

B         200   180

C         220   250

n=== 只看销售额 ===

region   North  South

product              

A          100    150

B          200    180

C          220    250

Code analysis:

  1. The original data is in long format, with each row recording the sales of a product in a certain region.
  2. Use set_index() Create a hierarchical index (product as the outer level, region as the inner level).
  3. unstack() Move the inner index region into the columns.
  4. The result is in wide format, allowing intuitive comparison of sales data for different products across regions.

Example 2: Unstacking a MultiIndex

When a DataFrame has multiple row index levels, you can specify which level to unstack.

Example

import

pandas

as

pd

1. Create data with a multi-level row index

df

=

pd.

DataFrame

(

{

   

'year'

:

[

2023

,

2023

,

2023

,

2023

,

2024

,

2024

,

2024

,

2024

]

,

   

'quarter'

:

[

'Q1'

,

'Q1'

,

'Q2'

,

'Q2'

,

'Q1'

,

'Q1'

,

'Q2'

,

'Q2'

]

,

   

'product'

:

[

'A'

,

'B'

,

'A'

,

'B'

,

'A'

,

'B'

,

'A'

,

'B'

]

,

   

'sales'

:

[

100

,

150

,

200

,

180

,

220

,

250

,

230

,

270

]

}

)

print

(

"=== Original data ==="

)

print

(

df

)

2. Set a multi-level index

df_indexed

=

df.

set_index

(

[

'year'

,

'quarter'

,

'product'

]

)

print

(

"n=== Set multi-level index ==="

)

print

(

df_indexed

)

3. Unstack the innermost level (product, default level=-1)

print

(

"n=== Unstack the product level (default) ==="

)

unstacked_inner

=

df_indexed.

unstack

(

)

print

(

unstacked_inner

[

'sales'

]

)

4. Unstack the middle level (quarter)

print

(

"n=== Unstack the quarter level ==="

)

unstacked_quarter

=

df_indexed.

unstack

(

level

=

'quarter'

)

print

(

unstacked_quarter

[

'sales'

]

)

5. Unstack the outer level (year)

print

(

"n=== Unstack the year level ==="

)

unstacked_year

=

df_indexed.

unstack

(

level

=

'year'

)

print

(

unstacked_year

[

'sales'

]

)

6. Unstack multiple levels at once

print

(

"n=== Unstack year and product together ==="

)

unstacked_multi

=

df_indexed.

unstack

(

level

=

[

'year'

,

'product'

]

)

print

(

unstacked_multi

[

'sales'

]

)

Expected output:



=== 原始数据 ===

   year quarter product  sales

0  2023      Q1       A    100

1  2023      Q1       B    150

2  2023      Q2       A    200

3  2023      Q2       B    180

4  2024      Q1       A    220

5  2024      Q1       B    250

6  2024      Q2       A    230

7  2024      Q2       B    270

n=== 设置多层索引 ===

                      sales

year quarter product       

2023 Q1      A          100

             B          150

     Q2      A          200

             B          180

2024 Q1      A          220

             B          250

     Q2      A          230

             B          270

n=== 展开 product 层(默认)===

product         A    B

year quarter          

2023 Q1       100  150

     Q2       200  180

2024 Q1       220  250

     Q2       230  270

n=== 展开 quarter 层 ===

quarter        Q1   Q2

year product          

2023 A        100  200

     B        150  180

2024 A        220  230

     B        250  270

n=== 展开 year 层 ===

year             2023  2024

quarter product            

Q1      A         100   220

        B         150   250

Q2      A         200   230

        B         180   270

n=== 同时展开 year 和 product ===

year    2023      2024     

product    A    B    A    B

quarter                    

Q1       100  150  220  250

Q2       200  180  230  270

Code analysis:

Example 3: Handling missing values with fill_value

Use fill_value The parameter can fill missing values produced after unstacking.

Example

import

pandas

as

pd

import

numpy

as

np

1. Create incomplete data (not every product-region combination has data)

df

=

pd.

DataFrame

(

{

   

'product'

:

[

'A'

,

'A'

,

'B'

,

'C'

]

,

   

'region'

:

[

'North'

,

'South'

,

'North'

,

'East'

]

,

   

'sales'

:

[

100

,

150

,

200

,

220

]

}

)

print

(

"=== Original data (incomplete) ==="

)

print

(

df

)

2. Create a hierarchical index and unstack

df_indexed

=

df.

set_index

(

[

'product'

,

'region'

]

)

print

(

"n=== Hierarchical index ==="

)

print

(

df_indexed

)

3. Do not fill missing values

print

(

"n=== Do not fill missing values ==="

)

unstacked

=

df_indexed.

unstack

(

)

print

(

unstacked

)

print

(

f

"Missing value count: {unstacked['sales'].isna().sum().sum()}"

)

4. Fill missing values with fill_value=0

print

(

"n=== fill_value=0 ==="

)

unstacked_fill

=

df_indexed.

unstack

(

fill_value

=

0

)

print

(

unstacked_fill

)

print

(

f

"Missing value count: {unstacked_fill['sales'].isna().sum().sum()}"

)

Expected output:



=== 原始数据(不完整)===

  product region  sales

0       A  North    100

1       A  South    150

2       B  North    200

3       C   East    220

n=== 层次化索引 ===

                sales

product region       

A       North     100

        South     150

B       North     200

C       East      220

n=== 不填充缺失值 ===

         sales              

region    East  North  South

product                     

A          NaN  100.0  150.0

B          NaN  200.0    NaN

C        220.0    NaN    NaN

缺失值数量: 5

n=== fill_value=0 ===

        sales            

region   East North South

product                  

A           0   100   150

B           0   200     0

C         220     0     0

缺失值数量: 0

Code analysis:

Example 4: Using unstack together with stack

unstack() and stack() They are inverse operations; used together they enable flexible data reshaping.

Example

import

pandas

as

pd

1. Create initial wide-format data

df

=

pd.

DataFrame

(

{

   

'product'

:

[

'A'

,

'B'

,

'C'

]

,

   

'North'

:

[

100

,

150

,

200

]

,

   

'South'

:

[

180

,

170

,

160

]

,

   

'East'

:

[

190

,

200

,

210

]

}

)

print

(

"=== Original wide-format data ==="

)

print

(

df

)

2. First use stack to convert to long format

print

(

"n=== step 1: stack converts to long format ==="

)

stacked

=

df.

set_index

(

'product'

)

.

stack

(

)

print

(

stacked

)

3. Then unstack back (to verify reversibility)

print

(

"n=== step 2: unstack converts back to wide format ==="

)

restored

=

stacked.

unstack

(

)

print

(

restored

)

4. Practical example: rearranging data

print

(

"n=== Practical example: moving region into columns ==="

)

Original: product as row index, region as columns

Goal: region as row index, product as columns

result

=

df.

set_index

(

'product'

)

.

unstack

(

)

.

unstack

(

level

=

1

)

print

(

result

)

5. Use reorder_levels to adjust the level order

print

(

"n=== Adjust levels with reorder_levels ==="

)

Create a two-level index

df_multi

=

pd.

DataFrame

(

   

[

[

100

,

200

]

,

[

150

,

180

]

,

[

190

,

210

]

]

,

    index

=

pd.

MultiIndex

.

from_tuples

(

[

       

(

'A'

,

'North'

)

,

(

'B'

,

'North'

)

,

(

'C'

,

'North'

)

   

]

,

names

=

[

'product'

,

'region'

]

)

,

    columns

=

[

'2023'

,

'2024'

]

)

print

(

"Original data:"

)

print

(

df_multi

)

Adjust the level order

print

(

"nAfter adjustment (year as the outer level):"

)

print

(

df_multi.

unstack

(

level

=

'region'

)

)

Expected output:



=== 原始宽格式数据 ===

  product  North  South  East

0       A    100    180   190

1        B    150    170 

...

Code analysis:

Tip: unstack() and stack() It is a core tool for data reshaping.unstack() splits rows into columns (long to wide),stack() piles columns into rows (wide to long).

Pandas Common Functions Common Pandas functions