1

我想重新调整列'w'

我已经平均了'w'

aveData_set = Data_Set.groupby(['buildingid', pd.Grouper(key='reporttime',freq='15T')])['w'].mean().reset_index()

aveData_set 结果:

aveData_set 结果

然后我想要每个24H重新缩放的列'w'

ScaleData_set = aveData_set.groupby(['buildingid', pd.Grouper(key='reporttime',freq='24H')])['w'].apply(lambda x: (x-x.min())/(x.max()-x.min())).reset_index()

但结果很奇怪,一些列消失了。

ScaleData_set 结果:

ScaleData_set 结果

我真的需要你的帮助。非常感谢。

4

1 回答 1

0

输出是预期的,因为缩放不是聚合值。它Series以与 original 相同的大小返回DataFrame

因此可以创建新列:

aveData_set['w_scaled'] = (aveData_set.groupby(['buildingid',
                                                pd.Grouper(key='reporttime',freq='1d')])['w']
                                      .apply(lambda x: (x-x.min())/(x.max()-x.min())))

或重新分配:

aveData_set['w'] = (aveData_set.groupby(['buildingid',
                                          pd.Grouper(key='reporttime',freq='1d')])['w']
                               .apply(lambda x: (x-x.min())/(x.max()-x.min())))

在这里apply工作,使用类似的 lambda 函数在这里transform查看更好的解释:

aveData_set['w'] = (aveData_set.groupby(['buildingid',
                                          pd.Grouper(key='reporttime',freq='1d')])['w']
                               .transform(lambda x: (x-x.min())/(x.max()-x.min())))

样品

rng = pd.date_range('2017-04-03 18:09:04', periods=10, freq='7T')
Data_Set = pd.DataFrame({'reporttime': rng, 'w': range(10), 'buildingid':[39] * 5 + [40] * 5})
print (Data_Set)
           reporttime  w  buildingid
0 2017-04-03 18:09:04  0          39
1 2017-04-03 18:16:04  1          39
2 2017-04-03 18:23:04  2          39
3 2017-04-03 18:30:04  3          39
4 2017-04-03 18:37:04  4          39
5 2017-04-03 18:44:04  5          40
6 2017-04-03 18:51:04  6          40
7 2017-04-03 18:58:04  7          40
8 2017-04-03 19:05:04  8          40
9 2017-04-03 19:12:04  9          40

aveData_set = (Data_Set.groupby(['buildingid', 
                                 pd.Grouper(key='reporttime',freq='15T')])['w']
                       .mean().reset_index())
print (aveData_set)

   buildingid          reporttime    w
0          39 2017-04-03 18:00:00  0.0
1          39 2017-04-03 18:15:00  1.5
2          39 2017-04-03 18:30:00  3.5
3          40 2017-04-03 18:30:00  5.0
4          40 2017-04-03 18:45:00  6.5
5          40 2017-04-03 19:00:00  8.5

aveData_set['w'] = (aveData_set.groupby(['buildingid',
                                          pd.Grouper(key='reporttime',freq='1d')])['w']
                               .apply(lambda x: (x-x.min())/(x.max()-x.min())))

print (aveData_set)
   buildingid          reporttime         w
0          39 2017-04-03 18:00:00  0.000000
1          39 2017-04-03 18:15:00  0.428571
2          39 2017-04-03 18:30:00  1.000000
3          40 2017-04-03 18:30:00  0.000000
4          40 2017-04-03 18:45:00  0.428571
5          40 2017-04-03 19:00:00  1.000000
于 2018-06-09T06:23:36.623 回答