我有一个dataframe (在Python上),列中包含与特定订单相关联的时间和产品数量。
我创建了一个列,该列将时间间隔为30分钟(时间块),然后在该特定时间块(使用groupby)上计算产品数量。
现在,我想添加一个累积频率,在每一个块上重新启动。

在这个例子中,我的新列应该是2,3,3,5,7,.
发布于 2022-02-08 23:52:43
您可以这样计算每个组的累积和:
df.groupby('Block of Time')['N Products'].cumsum()其结果如下:
0 2
1 3
2 3
3 5
4 7
Name: N Products, dtype: int64发布于 2022-02-08 23:32:35
GroupBy.cumsum()可以实现数据帧内每个时间块的累积和。
import pandas as pd
import numpy as np
df = pd.DataFrame({
'N Products' : [2, 1, 3, 5, 2],
'Block of Time' : ['12:30:00', '12:30:00', '13:30:00', '14:00:00', '14:00:00'],
'Products on Block': [3, 3, 3, 7, 7],
})
print(df)
# N Products Block of Time Products on Block
#0 2 12:30:00 3
#1 1 12:30:00 3
#2 3 13:30:00 3
#3 5 14:00:00 7
#4 2 14:00:00 7输出:
df['Cumulative Products'] = df.groupby('Block of Time')['N Products'].cumsum()
print(df)
# N Products Block of Time Products on Block Cumulative Products
#0 2 12:30:00 3 2
#1 1 12:30:00 3 3
#2 3 13:30:00 3 3
#3 5 14:00:00 7 5
#4 2 14:00:00 7 7民防部队阴谋:
df['Cumulative Products'].hist(cumulative = True)

https://stackoverflow.com/questions/71042053
复制相似问题