【问题标题】:How to rearrage the dataframe by combining 2 columns and sorting the corresponding rows to columns in pandas?如何通过组合 2 列并将相应的行排序到 pandas 中的列来重新排列数据框?
【发布时间】:2017-02-03 11:21:15
【问题描述】:

我有一个如下图所示的数据框:

Events   |  Start DateTime  |   End DateTime   | Max(in/hr) | Total Rainfall(in)
------------------------------------------------------------------------------
Event 1  | 2016-08-15 3:10  | 2016-08-15 19:08 | 3.67500     |     2.294
----------------------------------------------------------------------------
Event 2  | 2016-08-16 6:50  | 2016-08-17 12:57 | 1.41600     |     2.1680
-----------------------------------------------------------------------------
     |          |  
------------------------------------------------------------------------------
Event 47 | 2016-08-01 13:30 | 2016-08-01 17:03 | 0.1371      |     0.1023
------------------------------------------------------------------------------

我想重新排列数据框,使开始日期时间增加 5 分钟的时间间隔,直到结束日期时间,其他列也相应地排序。想要的输出如下图:

Time  |  Max (in/h) |   Total Rainfall(in)  | Events 
------------------------------------------------------------------------------
2016-08-15 3:10  |  3.67500 |     2.294     | Event 1
----------------------------------------------------------------------------
2016-08-15 3:15  |  3.67500 |     2.294     | Event 1     
-----------------------------------------------------------------------------
2016-08-15 3:20  |  3.67500 |     2.294     | Event 1       
------------------------------------------------------------------------------
||
------------------------------------------------------------------------------
2016-08-15 19:08 |  3.67500 |     2.294     | Event 1   (Upto End datetime)
-----------------------------------------------------------------------------
||
------------------------------------------------------------------------------
2016-08-01 17:03 |  0.1371  |     0.1023    | Event 47  (Similarly for all events)
------------------------------------------------------------------------------

【问题讨论】:

    标签: python-3.x pandas datetime


    【解决方案1】:

    首先通过melt 重新整形,然后通过groupbyresample 每个5 Minffill

    df['Start DateTime'] = pd.to_datetime(df['Start DateTime'])
    df['End DateTime'] = pd.to_datetime(df['End DateTime'])
    
    df1 = pd.melt(
        df, ['Max(in/hr)', 'Total Rainfall(in)', 'Events'],
        ['Start DateTime', 'End DateTime'],
        value_name='Time'
    ).drop('variable', 1).set_index('Time')
    print (df1)
                         Max(in/hr)  Total Rainfall(in)    Events
    Time                                                         
    2016-08-15 03:10:00      3.6750              2.2940   Event 1
    2016-08-16 06:50:00      1.4160              2.1680   Event 2
    2016-08-01 13:30:00      0.1371              0.1023  Event 47
    2016-08-15 19:08:00      3.6750              2.2940   Event 1
    2016-08-17 12:57:00      1.4160              2.1680   Event 2
    2016-08-01 17:03:00      0.1371              0.1023  Event 47
    
    df2 = df1.groupby('Events')
             .resample('5T')
             .ffill()
             .reset_index(level=0, drop=True)
             .reset_index()
    print (df2)
                       Time  Max(in/hr)  Total Rainfall(in)    Events
    0   2016-08-15 03:10:00      3.6750              2.2940   Event 1
    1   2016-08-15 03:15:00      3.6750              2.2940   Event 1
    2   2016-08-15 03:20:00      3.6750              2.2940   Event 1
    3   2016-08-15 03:25:00      3.6750              2.2940   Event 1
    4   2016-08-15 03:30:00      3.6750              2.2940   Event 1
    5   2016-08-15 03:35:00      3.6750              2.2940   Event 1
    6   2016-08-15 03:40:00      3.6750              2.2940   Event 1
    7   2016-08-15 03:45:00      3.6750              2.2940   Event 1
    8   2016-08-15 03:50:00      3.6750              2.2940   Event 1
    9   2016-08-15 03:55:00      3.6750              2.2940   Event 1
    ...
    ...
    

    【讨论】:

    • 我收到此错误:文件“”,第 2 行 .resample('5T') ^ IndentationError:意外缩进
    • 只能使用一行df2 = df1.groupby('Events').resample('5T').ffill().reset_index(level=0, drop=True).reset_index()
    • 或者在某些编辑器中检查缩进 - python 需要在每一行中使用相同的空格。
    • @nish - 非常感谢。您也可以投票支持我的旧解决方案。请尝试编辑this question,因为它稍后会被删除。谢谢。
    猜你喜欢
    • 2021-07-19
    • 2019-01-23
    • 1970-01-01
    • 2019-02-12
    • 2020-10-02
    • 2017-02-13
    • 2021-12-07
    • 2021-11-25
    • 2021-12-23
    相关资源
    最近更新 更多