【问题标题】:Pivot a dataframe with duplicate values in Index透视索引中具有重复值的数据框
【发布时间】:2015-10-04 04:10:11
【问题描述】:

我有一个这样的熊猫数据框

    snapDate     instance   waitEvent                   AvgWaitInMs
0   2015-Jul-03  XX         gc cr block 3-way               1
1   2015-Jun-29  YY         gc current block 3-way          2
2   2015-Jul-03  YY         gc current block 3-way          1
3   2015-Jun-29  XX         gc current block 3-way          2
4   2015-Jul-01  XX         gc current block 3-way          2
5   2015-Jul-01  YY         gc current block 3-way          2
6   2015-Jul-03  XX         gc current block 3-way          2
7   2015-Jul-03  YY         log file sync                   9
8   2015-Jun-29  XX         log file sync                   8
9   2015-Jul-03  XX         log file sync                   8
10  2015-Jul-01  XX         log file sync                   8
11  2015-Jul-01  YY         log file sync                   9
12  2015-Jun-29  YY         log file sync                   8

我需要把它转换成

snapDate        instance    gc cr block 3-way    gc current block 3-way  log file sync  
2015-Jul-03       XX              1                      Na                  8
2015-Jun-29       YY              Na                     2                   8 
2015-Jul-03       YY              Na                     1                   9
...

我尝试了 pivot 但它返回一个错误 dfWaits.pivot(索引 = 'snapDate',列 = 'waitEvent',值 = 'AvgWaitInMs') 索引包含重复条目,无法重塑

结果应该是另一个数据帧

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    这是将数据框重塑为类似于您想要的东西的一种方法。如果您对生成的数据框有任何其他特定要求,请告诉我。

    import pandas as pd
    
    # your data
    # ====================================
    print(df)
    
           snapDate instance               waitEvent  AvgWaitInMs
    0                                                            
    0   2015-Jul-03       XX       gc cr block 3-way            1
    1   2015-Jun-29       YY  gc current block 3-way            2
    2   2015-Jul-03       YY  gc current block 3-way            1
    3   2015-Jun-29       XX  gc current block 3-way            2
    4   2015-Jul-01       XX  gc current block 3-way            2
    5   2015-Jul-01       YY  gc current block 3-way            2
    6   2015-Jul-03       XX  gc current block 3-way            2
    7   2015-Jul-03       YY           log file sync            9
    8   2015-Jun-29       XX           log file sync            8
    9   2015-Jul-03       XX           log file sync            8
    10  2015-Jul-01       XX           log file sync            8
    11  2015-Jul-01       YY           log file sync            9
    12  2015-Jun-29       YY           log file sync            8
    
    # processing
    # ====================================
    df_temp = df.set_index(['snapDate', 'instance', 'waitEvent']).unstack().fillna(0)
    
    df_temp.columns = df_temp.columns.get_level_values(1).values
    
    df_temp = df_temp.reset_index('instance')
    
    print(df_temp)
    
                instance  gc cr block 3-way  gc current block 3-way  log file sync
    snapDate                                                                      
    2015-Jul-01       XX                  0                       2              8
    2015-Jul-01       YY                  0                       2              9
    2015-Jul-03       XX                  1                       2              8
    2015-Jul-03       YY                  0                       1              9
    2015-Jun-29       XX                  0                       2              8
    2015-Jun-29       YY                  0                       2              8
    

    【讨论】:

    • 几乎有一种方法可以重复 snapDate 而不是空白。
    • @jhon.smith 当然,只需添加 .reset_index()。我已经更新了代码。请看一看。
    • 嗯我的数据框有 12 行,但是当我尝试 unstack 操作时,结果数据框只有 6 行,不完全是我想要的。我的结果数据框也应该有 12 行
    • @jhon.smith 我认为您不能保留与原始 df 相同的行数,因为某些行必须移动到列。例如,假设Jul-03 数据,行0, 6, 9 都是与snapDate 实例XX 大致相同的记录。因此,进行数据透视会将这 3 行重塑为只有一行,因为这些数据已被移动到列中。
    【解决方案2】:

    你也可以使用pivot_table:

    df.pivot_table(index=['snapDate','instance'], columns='waitEvent', values='AvgWaitInMs')
    
    Out[64]:
    waitEvent             gc cr block 3-way  gc current block 3-way  log file sync
    snapDate    instance
    2015-Jul-01 XX                      NaN                       2              8
                YY                      NaN                       2              9
    2015-Jul-03 XX                        1                       2              8
                YY                      NaN                       1              9
    2015-Jun-29 XX                      NaN                       2              8
                YY                      NaN                       2              8
    

    数据:

    我使用以下 txt 文件作为输入(使用来自 pandasread_csv 来获取 data.frame):

    snapDate;instance;waitEvent;AvgWaitInMs
    0;2015-Jul-03;XX;gc cr block 3-way;1
    1;2015-Jun-29;YY;gc current block 3-way;2
    2;2015-Jul-03;YY;gc current block 3-way;1
    3;2015-Jun-29;XX;gc current block 3-way;2
    4;2015-Jul-01;XX;gc current block 3-way;2
    5;2015-Jul-01;YY;gc current block 3-way;2
    6;2015-Jul-03;XX;gc current block 3-way;2
    7;2015-Jul-03;YY;log file sync;9
    8;2015-Jun-29;XX;log file sync;8
    9;2015-Jul-03;XX;log file sync;8
    10;2015-Jul-01;XX;log file sync;8
    11;2015-Jul-01;YY;log file sync;9
    12;2015-Jun-29;YY;log file sync;8
    

    【讨论】:

    • 我得到 DataError: No numeric types to aggregate
    • 肯定是因为你的数据...你的专栏AvgWaitInMs是什么类型的?
    • snapDate 对象实例对象 waitEvent 对象 AvgWaitInMs 对象数据类型:对象
    • 你可以查看数据,我把它们放在我这边。实际上,OP 应该提供一个可重现的示例;)(就像一个复制/粘贴的字典)
    • 您好,我将 AvgWaitInMs 的数据类型更改为 int 并且枢轴工作
    猜你喜欢
    • 2015-09-08
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 2021-03-22
    • 2020-08-30
    • 1970-01-01
    相关资源
    最近更新 更多