在我获得df_base中所需的所有数据后(为了简单起见,我将不包括这些数据),我希望返回df_product_final,并包含以下列:
对于前两列,这不是问题,因为我只是从df_base复制列,并将它们粘贴到df_product_final中
对于SpeedAvg,我需要在df_product_final中插入该产品的平均速度,直到新产品出现在product列中
我的代码:
df_product_final['Product'] = df_product_total['Product']
df_product_final['Speed'] = df_base['production'] / df_base['time_production']
df_product_final=df_product_final.fillna(0)
df_product_final['SpeedAvg'] = df_product_final["Speed"].groupby(df_product_final['Product']).mean()
df_product_final['newindex'] = df_base['date_key']+df_base['hour']+df_base['minute']
df_product_final['newindex'] = pd.to_datetime(df_product_final['newindex'], utc=1, format = "%Y%m%d%H%M%S")
df_product_final.set_index('newindex',inplace=True)
df_product_final=df_product_final.fillna(0)
df_产品_最终版本:
newindex Product Speed SpeedAvg
2020-10-15 22:00:00+00:00 0 0.000000 52.944285
2020-10-15 23:00:00+00:00 0 0.000000 0.000000
2020-10-16 00:00:00+00:00 0 0.000000 0.000000
2020-10-16 01:00:00+00:00 0 0.000000 0.000000
2020-10-16 02:00:00+00:00 0 0.000000 0.000000
...
2020-10-16 20:00:00+00:00 0 154.000000 0.000000
2020-10-16 21:00:00+00:00 0 150.000000 0.000000
我希望得到以下结果:
newindex Product Speed SpeedAvg
2020-10-15 22:00:00+00:00 0 0.000000 52.944285
2020-10-15 23:00:00+00:00 0 0.000000 52.944285
2020-10-16 00:00:00+00:00 0 0.000000 52.944285
2020-10-16 01:00:00+00:00 0 0.000000 52.944285
...
2020-10-16 20:00:00+00:00 0 154.000000 52.944285
2020-10-16 21:00:00+00:00 0 0.000000 52.944285
为了使事情变得更加复杂,可能会有相同的产品,但会分离一个多小时。 在这种情况下,我的SpeedAvg取决于这些新值,而不是以前的值
例如:
Product Speed SpeedAvg
newindex
2020-10-15 22:00:00+00:00 0 0.000000 52.944285
2020-10-15 23:00:00+00:00 0 0.000000 52.944285
2020-10-16 00:00:00+00:00 0 0.000000 52.944285
2020-10-16 01:00:00+00:00 0 0.000000 52.944285
2020-10-16 02:00:00+00:00 1 10.000000 10.000000
2020-10-16 03:00:00+00:00 1 10.000000 10.000000
2020-10-16 04:00:00+00:00 1 10.000000 10.000000
2020-10-16 05:00:00+00:00 1 10.000000 10.000000
2020-10-16 06:00:00+00:00 1 10.000000 10.000000
2020-10-16 07:00:00+00:00 0 0.000000 31.500000
2020-10-16 08:00:00+00:00 0 0.000000 31.500000
2020-10-16 16:00:00+00:00 0 183.000000 31.500000
2020-10-16 17:00:00+00:00 0 69.000000 31.500000
2020-10-16 18:00:00+00:00 0 0.000000 31.500000
2020-10-16 19:00:00+00:00 0 0.000000 31.500000
2020-10-16 20:00:00+00:00 0 0.000000 31.500000
2020-10-16 21:00:00+00:00 0 0.000000 31.500000
如果我不是很全面,我很抱歉,我会提供解决这个问题所需的每一点信息
找到另一个使用分组方式的解决方案。如果这对你有用的话
改编自对this post的回答
我想我找到了一个更容易解决问题的方法:
从一个空字典开始,我将df_base的所有键插入其中,如下所示:
生成的字典将类似于:
使用df.apply()完成此步骤后,我可以迭代dataframe的每一行,使用刚刚创建的字典的键更改产品键的行值:
self.keys_from_值:
最后一步是计算并在数据帧内插入正确的SpeedAvg(这很容易:第一个循环是基于刚刚修改的行获取列组id;第二个循环是基于组id插入SpeedAvg):
这是经过以下步骤后生成的数据帧(df_product_final):
相关问题 更多 >
编程相关推荐