【问题标题】:Mapping the values of one dataframe using the index to other dataframe column使用索引将一个数据框的值映射到其他数据框列
【发布时间】:2019-09-23 16:40:54
【问题描述】:

我有一个数据框 df1,它包含三列(目标、季节性和季节性指数)。季节性模式每 7 个点重复一次。季节性的最后一个索引是 2。我有另一个数据框 df2,它的预测列有 10 行。现在我想在 df2 中创建新列,这将是 df1 的预测列和季节性列的总和。这里的问题是映射。我想使用带有相应季节性列的季节性索引列将值添加到预测列。例如:第 4 个季节性指数的季节性值应添加到预测列的第一个元素。用完第 6 个索引值后,随着季节性在 7 点后重复,加法应再次从零开始。

df1

                       Target   Seasonality Seasonality_index
Datetime            
2019-01-01 00:00:00     0.44    0.12    0
2019-01-01 01:00:00     0.44    0.06    1
2019-01-01 02:00:00     0.43    0.01    2
2019-01-01 03:00:00     0.43    -0.04   3
2019-01-01 04:00:00     0.43    -0.09   4
2019-01-01 05:00:00     0.43    -0.10   5
2019-01-01 06:00:00     0.42    -0.13   6
2019-01-01 07:00:00     0.42    0.12    0
2019-01-01 08:00:00     0.42    0.06    1
2019-01-01 09:00:00     0.43    0.01    2


df2

       Datetime         forecasted   Expected_output
    2019-01-01 10:00:00 7.21         7.21 -(-0.04) #4th element
    2019-01-01 11:00:00 7.20         7.20 -(-0.09) #5th element
    2019-01-01 12:00:00 7.19         7.19 -(-0.10) #6th element
    2019-01-01 13:00:00 7.18         7.18 -(-0.13) #7th element
    2019-01-01 14:00:00 7.19         7.19 -(0.12) #1st element
    2019-01-01 15:00:00 7.19         7.19 -(0.06) #2nd element
    2019-01-01 16:00:00 7.20         7.20 -(-0.10) #3rd element
    2019-01-01 17:00:00 7.20         7.20 -(-0.04) #4th element
    2019-01-01 18:00:00 7.21         7.21 -(-0.09) #5th element
    2019-01-01 19:00:00 7.20         7.20 -(-0.10) #6th element

【问题讨论】:

  • 你的预期输出是什么?
  • @ Vikas P 我创建了一个单独的列来描述我的预期输出。希望能澄清您的问题。
  • want to add values to forecasted column using seasonality index column with corresponding seasonality column. - 问题在于样本数据中的第一个 df1 不是 (145th index value of seasonality)(146th index value of seasonality)...。我建议创建minimal, complete, and verifiable example,包含 5、3 行样本数据,并添加预期输出以便于验证解决方案。
  • @jezrael 根据您的建议,我创建了一个示例数据,我想这个更具体
  • @jezreal 我已经编辑了预期的输出。请检查

标签: python pandas datetime


【解决方案1】:

我相信你可以使用:

repeat = df['Seasonality_index'].max() + 1

#first convert first group values to list
a = df1['Seasonality'].tolist()[:repeat]
print (a)
[0.12, 0.06, 0.01, -0.04, -0.09, -0.1, -0.13]

#reorder values by constant
first = df['Seasonality_index'].iat[-1] + 1
b= a[first:] + a[:first]
print (b)
[-0.04, -0.09, -0.1, -0.13, 0.12, 0.06, 0.01]

#repeat values by length of df2
arr = np.tile(b, int(len(df2) // repeat) + repeat)
#assign by length of df2
df2['test'] = arr[:len(df2)]
df2['Expected_output'] = df2['forecasted']  - arr[:len(df2)]

print (df2)
                     forecasted  Expected_output  test
Datetime                                              
2019-01-01 10:00:00        7.21             7.25 -0.04
2019-01-01 11:00:00        7.20             7.29 -0.09
2019-01-01 12:00:00        7.19             7.29 -0.10
2019-01-01 13:00:00        7.18             7.31 -0.13
2019-01-01 14:00:00        7.19             7.07  0.12
2019-01-01 15:00:00        7.19             7.13  0.06
2019-01-01 16:00:00        7.20             7.19  0.01
2019-01-01 17:00:00        7.20             7.24 -0.04
2019-01-01 18:00:00        7.21             7.30 -0.09
2019-01-01 19:00:00        7.20             7.30 -0.10

【讨论】:

  • @Jezrael 这很有用。但是,这是您使用 first = 3 的特定情况,如果我不想硬编码任何东西怎么办?无论我最后的季节性指数是什么,df2 中的添加仅从那里开始?
  • @AB14 - 然后使用first = df['Seasonality_index'].iat[-1] + 1
  • @AB14 - 而不是a = df1['Seasonality'].tolist()[:7] 使用a = df1['Seasonality'].tolist()[:repeat]
  • 还有arr = np.tile(b, int(len(df2) // repeat) + repeat)
猜你喜欢
  • 1970-01-01
  • 2020-06-21
  • 2017-01-16
  • 1970-01-01
  • 1970-01-01
  • 2020-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多