我试图对数据帧中的每一行进行多行计算。
我目前的解决方案需要2个小时才能处理20万行。所以效率很低,我希望groupby或者其他一些pandas方法能帮到我。
我的数据如下所示(您可以暂时忽略日期):
id group start_date end_date three_yrs_ago_date days_missing
01 23 2005-01-01 2006-01-01 2002-01-01 1
02 23 2006-01-06 2007-01-06 2003-01-06 6
03 23 2007-01-15 2008-01-15 2004-01-15 9
07 17 2014-01-01 2015-02-01 2011-01-01 2
07 23 2015-01-01 2016-02-01 2012-01-01 4
所以这里的目标是按
group
然后把所有的
days_missing
在过去三年中发生的所有其他行中。也就是说其他几行
start_date
在当前行的位置或之后
three_yrs_ago_date
,并且在当前行的
end_date
.
这是一口,但基本上有三个标准。因此,如果这是整个数据集,我们将得到这个结果(删除日期列):
id group days_missing days_missing_in_last_three_years
01 23 1 1 # no change: no prior years
02 23 6 7
03 23 9 16
07 17 2 2 # no change: only member of it's group
07 23 4 4 # no change: other