如何通过组合2列并将相应的行排序到pandas中的列来重置数据帧?

时间:2017-02-03 11:21:16

标签: python-3.x pandas datetime

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

if( ! empty($age)) {
echo '<p>'.__('Age','framework').': <span>'.$age.'</span></p>';
}

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

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
------------------------------------------------------------------------------

1 个答案:

答案 0 :(得分: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
...
...