【发布时间】:2020-02-08 08:54:19
【问题描述】:
date value pointName pointNr connectedPoint ownerName
2018-05-08 2.039373e+08 Miami_1 P-00068 Point_1 Owner_1
2018-05-09 2.546125e+08 Miami_1 P-00068 Point_1 Owner_1
2018-05-09 2.546010e+08 Miami_2 P-00066 Point_1 Owner_2
2018-05-08 2.037412e+08 Miami_2 P-00066 Point_1 Owner_2
2018-05-09 7.142878e+08 New_York_1 P-00211 Point_2 Owner_3
2018-05-08 6.567392e+08 New_York_1 P-00211 Point_2 Owner_3
2018-05-08 6.567392e+08 New_York_2 P-00188 Point_2 Owner_4
2018-05-09 7.141274e+08 New_York_3 P-00126 Point_2 Owner_2
2018-05-09 7.142878e+08 New_York_2 P-00188 Point_2 Owner_4
2018-05-08 6.566841e+08 New_York_3 P-00126 Point_2 Owner_2
2018-05-08 0.000000e+00 Boston_1 P-00081 Point_3 Owner_4
2018-05-08 0.000000e+00 Boston_2 P-00105 Point_3 Owner_5
2018-05-09 6.987462e+07 Boston_2 P-00105 Point_3 Owner_5
2018-05-09 7.000680e+07 Boston_1 P-00081 Point_3 Owner_4
上面的sn-p或多或少是以下结果:
rng = pd.DataFrame(my_df[['date', 'value', 'pointName', 'pointNr', 'connectedPoint', 'ownerName]].sort_values('connectedPoint').reset_index(drop=True))
rng.head(14)
我得到了全年的数据。对于这个例子,我选择了两天(2018-05-08 和 2018-05-09)
我想计算一个时间范围内的总和(在本例中:两天),但每天只计算每个 connectedPoint 的最大值。
2018-05-08 伪数学写作示例:
总和(最大{Point1} + 最大{Point2} + 最大{Point3})
= 2.039373e+08 + 6.567392e+08 + …
最后,我们将每一天(第 1 天 + 第 2 天 + 第 3 天……)的值(即之前计算的总和)相加,得出一个最终值。
我尝试了使用 groupby 的不同方法,以及以下变体:
rng['date'] = pd.to_datetime(rng['date'])
rng.index = rng['date']
rng.resample('D').max()
抱歉,我是 python 和 pandas 的新手。我在网上搜索过,但即使这种情况对你们中的许多人来说很明显,我仍然找不到解决方案。我被困住了。
提前致谢!
【问题讨论】:
-
请复制并粘贴您的 df
标签: pandas date sum time-series pandas-groupby