pandas之stack与unstack

stack: 将列转行。转哪个层级由参数 level 决定,该 level 指的是 columns 所对应的层级

import numpy as np
import pandas as pd
from pandas import Dataframe

index = pd.MultiIndex.from_arrays([['a', 'a', 'b', 'b'], [1, 2, 1, 2]])
columns = pd.MultiIndex.from_product([list("ABC"), ["one", "two"]])
df = DataFrame(np.arange(24).reshape(4, 6), index=index, columns=columns)

In[222]: df
Out[222]: 
      A       B       C      # columns 的 level 为从上往下依次递增
    one two one two one two
a 1   0   1   2   3   4   5
  2   6   7   8   9  10  11
b 1  12  13  14  15  16  17
  2  18  19  20  21  22  23
  
In[223]: df.stack(0)  # 将列的第一级转到行。
Out[223]: 
       one  two
a 1 A    0    1
    B    2    3
    C    4    5
  2 A    6    7
    B    8    9
    C   10   11
b 1 A   12   13
    B   14   15
    C   16   17
  2 A   18   19
    B   20   21
    C   22   23
    
In[224]: df.stack(1)  # 将列的第二级转到行
Out[224]: 
          A   B   C
a 1 one   0   2   4
    two   1   3   5
  2 one   6   8  10
    two   7   9  11
b 1 one  12  14  16
    two  13  15  17
  2 one  18  20  22
    two  19  21  23

unstack: 将行转列。转哪个层级由参数 level 决定,该 level 指的是 index 所对应的层级

In[225]: df
Out[225]: 
      A       B       C      # index 的 level 为从外往内依次递增
    one two one two one two
a 1   0   1   2   3   4   5
  2   6   7   8   9  10  11
b 1  12  13  14  15  16  17
  2  18  19  20  21  22  23
  
In[226]: df.unstack(0)  # 将行的第一级转到列
Out[226]: 
    A               B               C            
  one     two     one     two     one     two    
    a   b   a   b   a   b   a   b   a   b   a   b
1   0  12   1  13   2  14   3  15   4  16   5  17
2   6  18   7  19   8  20   9  21  10  22  11  23

In[227]: df.unstack(1)  # 将行的第二级转到列
Out[227]: 
    A               B               C            
  one     two     one     two     one     two    
    1   2   1   2   1   2   1   2   1   2   1   2
a   0   6   1   7   2   8   3   9   4  10   5  11
b  12  18  13  19  14  20  15  21  16  22  17  23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值