【发布时间】:2017-12-05 10:52:21
【问题描述】:
假设你有这个 DataFrame:
Name Item Date value1 value2
Marc bike 21-Dec-17 7 1000
Marc bike 05-Jan-18 9 2000
Marc bike 27-Jul-18 4 500
John house 14-Dec-17 4 500
John house 02-Feb-18 6 500
John house 07-Feb-18 8 1000
John house 16-Feb-18 2 1000
John house 05-Dec-21 7 1000
John house 27-Aug-25 8 500
John car 17-Apr-18 4 500
我想将 value1 和 value2 放入每个 name-item-combination 的每月存储桶中(接下来 48 个月的每个第三个星期三)。
因此,每个组合有 49 个时间桶,每个月的 value1 和 value2 之和:Marc/bike、John/house、John/car、...
John/house 的解决方案如下所示:
Name Item TimeBucket value1 value2
John house 20-Dec-17 4 500
John house 17-Jan-18 0 0
John house 21-Feb-18 16 2500
John house 21-Mar-18 0 0
John house 18-Apr-18 0 0
John house … 0 0
John house 17-Nov-21 0 0
John house 15-Dec-21 7 1000
John house rest 8 500
我无法使用 pandas 获得结果。我能想到的唯一解决方案是通过数据帧逐行迭代,但我真的很想避免这样做。有什么优雅的方法吗?
【问题讨论】:
标签: python pandas grouping binning