【问题标题】:Specific time series dataframe with PandasPandas 的特定时间序列数据框
【发布时间】:2020-05-07 07:40:13
【问题描述】:

我有一年这样的 5 分钟数据:

df = pd.DataFrame([['1/1/2019 00:05', 1], ['1/1/2019 00:10', 5],['1/1/2019 00:15', 1], ['1/1/2019 00:20',3], ['1/1/2019 00:25', 1],
                   ['1/1/2019 00:30', 2], ['1/1/2019 00:35', 6],['1/1/2019 00:40', 8],['1/1/2019 00:45', 1], ['1/1/2019 00:55', 2],
                   ['1/1/2019 01:00', 8],['1/1/2019 01:05', 1], ['1/1/2019 01:10', 5],['1/1/2019 01:15', 1], ['1/1/2019 01:20',3],['1/1/2019 01:25', 1],
                   ['1/1/2019 01:30', 2], ['1/1/2019 01:35', 6],['1/1/2019 01:40', 8],['1/1/2019 01:45', 1], ['1/1/2019 01:55', 2],
                   ['1/1/2019 02:00', 8]],
                  columns = ['Date','Value'])

我希望在所有相应期间每小时转换一次。现在,每一行对应于特定日期和特定月份的一小时。像这样的:

df = pd.DataFrame([['day1hour0month1', 1, 1, 3, 4, 1, 0, 1, 5, 2, 1, 3,3],  ['day1hour1month1', 1, 1, 3, 4, 1, 0, 1, 5, 2, 1, 3,3], 
                   ['day1hour2month1', 1, 1, 3, 4, 1, 0, 1, 5, 2, 1, 3,3], ['day1hour3month1', 1, 1, 3, 4, 1, 0, 1, 5, 2, 1, 3,3], 
                   ['day1hour4month1', 1, 1, 3, 4, 1, 0, 1, 5, 2, 1, 3,3], ['day1hour5month1', 1, 1, 3, 4, 1, 0, 1, 5, 2, 1, 3,3], 
                   ['day1hour6month1', 1, 1, 3, 4, 1, 0, 1, 5, 2, 1, 3,3], ['day1hour7month1', 1, 1, 3, 4, 1, 0, 1, 5, 2, 1, 3,3], 
                   ['day1hour8month1', 1, 1, 3, 4, 1, 0, 1, 5, 2, 1, 3,3], ['day1hour9month1', 1, 1, 3, 4, 1, 0, 1, 5, 2, 1, 3,3], 
                   ['day31hour23month12', 1, 1, 8, 0, 6, 5, 3, 1, 1, 2,3,5]],
                  columns = ['Date', 'min05', 'min10', 'min15', 'min20', 'min25', 
                             'min30', 'min35', 'min40', 'min45', 'min50',
                             'min55', 'min60'])

有没有办法使用 Pandas 时间序列功能(不使用 for 循环)来做到这一点?对于实施此操作的任何建议,我将不胜感激。

提前感谢您!

干杯。

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    根据您的示例数据框:

    In [2213]: df['Date'] = pd.to_datetime(df['Date'])
    In [2191]: df1['dmh'] = 'day' + df.Date.dt.day.astype(str) + 'hour' + df.Date.dt.hour.astype(str) + 'month' + df.Date.dt.month.astype(str)
    
    In [2199]: df['minute'] = 'min' + df.Date.dt.minute.astype(str)
    
    In [2211]: df.pivot(index='dmh', columns='minute', values='Value')                                                                                                                                          
    Out[2211]: 
    minute           min0  min10  min15  min20  min25  min30  min35  min40  min45  min5  min55
    dmh                                                                                       
    day1hour0month1   NaN    5.0    1.0    3.0    1.0    2.0    6.0    8.0    1.0   1.0    2.0
    day1hour1month1   8.0    5.0    1.0    3.0    1.0    2.0    6.0    8.0    1.0   1.0    2.0
    day1hour2month1   8.0    NaN    NaN    NaN    NaN    NaN    NaN    NaN    NaN   NaN    NaN
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-11
      • 2016-01-30
      • 2020-12-21
      • 2016-02-01
      • 1970-01-01
      • 2018-02-21
      • 2022-11-27
      相关资源
      最近更新 更多