我有两个数据帧。我想在第二列中为第一个datafame中的每条记录加一个“amount”列。你知道吗
所以对于每个
df1.Date = sum(df2.amount WHERE df1.Date <= df2.Date AND df1.yearAgo >= df2.Date)
df1 = pd.DataFrame({'Date':['2018-10-31','2018-10-30','2018-10-29','2018-10-28'],'yearAgo':['2017-10-31','2017-10-30','2017-10-29','2017-10-28']})
df2 = pd.DataFrame({'Date':['2018-10-30','2018-7-30','2018-4-30','2018-1-30','2017-10-30'],'amount':[1.0,1.0,1.0,1.0,0.75]})
预期结果:
df1.Date yearToDateTotalAmount
2018-10-31 3.0
2018-10-30 4.75
2018-10-29 3.75
2018-10-28 3.75
IIUC,预期输出的第一行应该有
4
。你知道吗使用} 比较特性可以非常有效地实现这一点,因为
numpy
的^{less_equal
和greater_equal
是ufunc
注意
所以你可以在
使用^{} +沿
axis=1
求和最后,只要分配回来
相关问题 更多 >
编程相关推荐