如何使用python来分组和缩放值?

2024-09-30 18:15:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我想重新缩放列'w'。你知道吗

我平均了'w'。你知道吗

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

aveData设置结果:

aveData_set result

然后我希望每个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 result

我真的需要你的帮助救命啊。很多谢谢。你知道吗


Tags: keydataindexminmeanpdresetfreq
1条回答
网友
1楼 · 发布于 2024-09-30 18:15:38

输出是预期的,因为缩放不是聚合值。它返回与原始DataFrame大小相同的Series。你知道吗

因此可以创建新列:

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())))

这里^{}^{}一样工作,用类似的lambda函数检查更好的解释here

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

相关问题 更多 >