我的目标是通过给出行上的值(让我们说3),查找下面3行的给定列的值。目前我正在使用for循环,但效率非常低。
我已经读过矢量化可以帮助解决这个问题,但我不确定如何。
我的数据是这样的:
Date DaysToReception Quantity QuantityAtTheEnd
20/03 3 102
21/03 - 88
22/03 - 57
23/03 5 178
24/03
我想获得:
Date DaysToReception Quantity QuantityAtReception
20/03 3 102 178
21/03 - 88
22/03 - 57
23/03 5 178
24/03
...
感谢您的帮助!
答案 0 :(得分:0)
如果您有唯一的日期或DaysToReception,您实际上可以使用Map / HashMap,其中键将是日期或DaysToReception,值将是您可以使用列表或任何其他适当的数据结构存储的其他信息。
这肯定会提高效率。
正如您所指出的那样,“我搜索下面的值的行数取决于值”DaysToReception“,我相信”DaysToReception“不会是唯一的。在这种情况下,Map的关键是日期。< / p>
答案 1 :(得分:0)
我能想到的最简单的方法是在熊猫中做到这一点:
# something like your dataframe
df = pd.DataFrame(dict(date=['20/03', '21/03', '22/03', '23/03'],
days=[3, None, None, 5,],
quant=[102, 88, 57, 178]))
# get the indexs of all days that aren't missing
idxs = df.index[~pd.isnull(df.days)]
# get number of days to go
values = df.days[idxs].values.astype(int)
# get index of three days ahead
new_idxs = idxs+values
# create a blank column
df['quant_end'] = None
# Now fill it with the data we're after
df.quant_end[idxs] = df.quant[new_idxs]