Pandas 熊猫:考虑不同实体的最大值的时间范围总和
上面的片段或多或少是:Pandas 熊猫:考虑不同实体的最大值的时间范围总和,pandas,date,sum,time-series,pandas-groupby,Pandas,Date,Sum,Time Series,Pandas Groupby,上面的片段或多或少是: 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
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
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)
我想计算一个时间范围内的总和(本例中为两天),但每天只计算每个连接点的最大值。2018-05-08伪数学写作示例:
求和(max{Point1}+max{Point2}+max{Point3})
=2.039373e+08+6.567392e+08+…
最后,我们对每天(第1天+第2天+第3天…)的值(指计算前的总和)进行汇总,得出一个最终值 我尝试了groupby的不同方法,以及:
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
rng = pd.DataFrame(my_df[['date', 'value', 'pointName', 'pointNr', 'connectedPoint', 'ownerName]].sort_values('connectedPoint').reset_index(drop=True))
rng.head(14)
对不起,我对蟒蛇和熊猫还不熟悉。我在网上搜索过,但仍然找不到解决方案,即使这个案例对你们中的许多人来说是显而易见的。我卡住了
提前谢谢 使用+
按日期获取总金额:
connectedPoint Point_1 Point_2 Point_3
date
2018-05-08 203937300.0 656739200.0 0.0
2018-05-09 254612500.0 714287800.0 70006800.0
sum_date=df1.sum(axis=1)
print(sum_date)
date
2018-05-08 8.606765e+08
2018-05-09 1.038907e+09
dtype: float64
获取总金额:
connectedPoint Point_1 Point_2 Point_3
date
2018-05-08 203937300.0 656739200.0 0.0
2018-05-09 254612500.0 714287800.0 70006800.0
sum_date=df1.sum(axis=1)
print(sum_date)
date
2018-05-08 8.606765e+08
2018-05-09 1.038907e+09
dtype: float64
请多复制粘贴你的照片!我试过了,它成功了。不知道unstack()。有很多东西需要阅读和发现;)我很乐意帮忙:)