将小时(00到23)添加到日期列

时间:2017-10-10 10:18:03

标签: python pandas time-series

我处理过一个数据集并且能够以这种方式重新排列它(pastebin)。

         date type  value location
0  2017-10-03    D   3035    INDIA
0  2017-10-03    D   3035    INDIA
0  2017-10-03    D   3035    INDIA
0  2017-10-03    D   3035    INDIA
0  2017-10-03    D   3035    INDIA
   ................................
   total 24 rows for D type and India
   ................................
1  2017-10-03    F   3035    INDIA
1  2017-10-03    F   3035    INDIA
1  2017-10-03    F   3035    INDIA
1  2017-10-03    F   3035    INDIA
1  2017-10-03    F   3035    INDIA
   ................................
   total 24 rows for F type and India
   ................................
2  2017-10-03    D   3035    CHINA
2  2017-10-03    D   3035    CHINA
2  2017-10-03    D   3035    CHINA
2  2017-10-03    D   3035    CHINA
2  2017-10-03    D   3035    CHINA
   ................................
   total 24 rows for D type and China
   ................................

预期输出:我想将小时(从00:00到23:00)添加到日期列。 date, type, location正好有24行。所以我只需要遍历日期列,前24行,追加时间(00:00到23:00),然后对接下来的24行进行相同操作等等......

0  2017-10-03 00:00   D   3035    INDIA
0  2017-10-03 01:00   D   3035    INDIA
0  2017-10-03 02:00   D   3035    INDIA
0  2017-10-03 03:00   D   3035    INDIA
0  2017-10-03 04:00   D   3035    INDIA
   ................................
   total 24 rows for D type and India
   ................................
1  2017-10-03 01:00    F   3035    INDIA
1  2017-10-03 02:00    F   3035    INDIA
1  2017-10-03 03:00    F   3035    INDIA
1  2017-10-03 03:00    F   3035    INDIA
1  2017-10-03 04:00    F   3035    INDIA

1 个答案:

答案 0 :(得分:3)

我认为您可以使用groupby索引与cumcount进行统计,然后将其转换为hour to_timedelta,并将其添加到date s:< / p>

#convert to datetime if necessary
df['date'] = pd.to_datetime(df['date'])

df['date'] += pd.to_timedelta(df.groupby(level=0).cumcount(), unit='H')
print (df.head(30))

                 date type  value location
0 2017-10-03 00:00:00    D   3035    INDIA
0 2017-10-03 01:00:00    D   3035    INDIA
0 2017-10-03 02:00:00    D   3035    INDIA
0 2017-10-03 03:00:00    D   3035    INDIA
0 2017-10-03 04:00:00    D   3035    INDIA
0 2017-10-03 05:00:00    D   3035    INDIA
0 2017-10-03 06:00:00    D   3035    INDIA
0 2017-10-03 07:00:00    D   3035    INDIA
0 2017-10-03 08:00:00    D   3035    INDIA
0 2017-10-03 09:00:00    D   3035    INDIA
0 2017-10-03 10:00:00    D   3035    INDIA
0 2017-10-03 11:00:00    D   3035    INDIA
0 2017-10-03 12:00:00    D   3035    INDIA
0 2017-10-03 13:00:00    D   3035    INDIA
0 2017-10-03 14:00:00    D   3035    INDIA
0 2017-10-03 15:00:00    D   3035    INDIA
0 2017-10-03 16:00:00    D   3035    INDIA
0 2017-10-03 17:00:00    D   3035    INDIA
0 2017-10-03 18:00:00    D   3035    INDIA
0 2017-10-03 19:00:00    D   3035    INDIA
0 2017-10-03 20:00:00    D   3035    INDIA
0 2017-10-03 21:00:00    D   3035    INDIA
0 2017-10-03 22:00:00    D   3035    INDIA
0 2017-10-03 23:00:00    D   3035    INDIA
1 2017-10-03 00:00:00    F   3035    INDIA
1 2017-10-03 01:00:00    F   3035    INDIA
1 2017-10-03 02:00:00    F   3035    INDIA
1 2017-10-03 03:00:00    F   3035    INDIA
1 2017-10-03 04:00:00    F   3035    INDIA
1 2017-10-03 05:00:00    F   3035    INDIA