在 pandas 中从宽到长重塑 [英] Reshape wide to long in pandas

查看:23
本文介绍了在 pandas 中从宽到长重塑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在 Pandas 中有以下数据框:

Let's assume that I have the following dataframe in pandas:

             AA  BB  CC     
   date
   05/03     1   2   3  
   06/03     4   5   6  
   07/03     7   8   9  
   08/03     5   7   1  

我想将其转换为以下内容:

and I want to transform it to the following:

   AA 05/03    1
   AA 06/03    4
   AA 07/03    7
   AA 08/03    5
   BB 05/03    2
   BB 06/03    5
   BB 07/03    8
   BB 08/03    7
   CC 05/03    3
   CC 06/03    6
   CC 07/03    9
   CC 08/03    1

我该怎么做?

从宽转换为长的原因是,在下一阶段,我想根据日期和初始列名(AA、BB、CC)将此数据框与另一个数据框合并.

The reason of the transformation from wide to long is that, in the next stage, I would like to merge this dataframe with another one, based on dates and the initial column names (AA, BB, CC).

推荐答案

更新

正如 George Liu 在另一个答案中所示,pd.melt 是惯用语,灵活快速地解决这个问题.不要为此使用unstack.

Update

As George Liu has shown in another answer, pd.melt is the idiomatic, flexible and fast solution to this problem. Do not use unstack for this.

unstack 返回具有多重索引的系列:

unstack returns a series with a multiindex:

    In [38]: df.unstack()
    Out[38]: 
        date 
    AA  05/03    1
        06/03    4
        07/03    7
        08/03    5
    BB  05/03    2
        06/03    5
        07/03    8
        08/03    7
    CC  05/03    3
        06/03    6
        07/03    9
        08/03    1
    dtype: int64

您可以在返回的系列上调用 reset_index:

You can call reset_index on the returning series:

In [39]: df.unstack().reset_index() 
Out[39]:        
        
    level_0 date    0
0   AA      05-03   1
1   AA      06-03   4
2   AA      07-03   7
3   AA      08-03   5
4   BB      05-03   2
5   BB      06-03   5
6   BB      07-03   8
7   BB      08-03   7
8   CC      05-03   3
9   CC      06-03   6
10  CC      07-03   9
11  CC      08-03   1

或者构造一个带有多索引的数据框:

Or construct a dataframe with a multiindex:

In [40]: pd.DataFrame(df.unstack())     
Out[40]:        
        
            0
    date    
AA  05-03   1
    06-03   4
    07-03   7
    08-03   5
BB  05-03   2
    06-03   5
    07-03   8
    08-03   7
CC  05-03   3
    06-03   6
    07-03   9
    08-03   1

这篇关于在 pandas 中从宽到长重塑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆