Pandas - 使用相应列中的值将数据帧的频率从每天更改为每小时

Pandas - Changing the frequency of a dataframe from daily to hourly, using the values from the corresponding columns

我有一个包含每日值的数据框,列对应于一天中每小时的测量值,每小时一列。相反,我想从每天到每小时对值进行重新采样,并将列数减少到 1。但是我完全不知道如何执行此操作。

假设您想删除非小时列,解决方案是:

result = df.set_index('date')\
    .filter(regex='^h')\
    .stack()\
    .to_frame()

# index values are now tuples such as (2018-01-01, 'h1')
result = result.set_index(result.index.map(
    lambda idx: idx[0] + timedelta(hours=int(idx[1][1:]))
))

下面的代码块创建了一个包含两列的新 DataFrame:

  • ID:此 ID 是 zone_id、日期和当天的小时的组合。
  • 观察:这是属于特定时间(ID)的观察。

代码:

new_data=[]
for index,row in your_DataFrame.iterrows(): 
    zone_id_date=str(row['zone_id'])+'_'+str(row['date']) 
    for hour in range(1,25): 
        ID=zone_id_date+'_h'+str(hour) 
        observation=row['h'+str(hour)] 
        new_row=[ID,observation] 
        new_data.append(new_row) 

output_data=pandas.DataFrame(data=new_data, columns = ['ID', 'observation'])