我是Python和Pandas的新手,所以这是我的问题:
我有两个数据范围,DF1有两个用于标签的列,一个用于整数,而与每个标签的toal相对应,而DF2包含白天使用的数量。我想减去DF2的每一行,直到DF1等于或接近0,然后将列添加到DF1的最后一行的日期(可以在新的DataFrame DF3中)。减法需要有两个条件:
- 标签需要相等的
- 减法不能少量到0
df1 = df总计
df2 = df的数量
我希望有人可以帮助我。
I'm new with python and pandas so here's my question:
I have two dataframes, df1 has two columns one for labels and one for integers which correspond to the Toal of each label while df2 contains the quantity used by day. I would like to subtract each row of df2 until df1 is equal or closer to 0 and add a column to df1 the date of the last row subtracted (it could be in a new dataframe df3). The subtraction needs to have two conditions where:
- labels need to be equal
- subtraction can not be minor to 0
df1 = DF with Totals
df2 = DF with quantities
I hope someone can help me.
发布评论
评论(1)
我相信.cumsum()和.idxmin()将有所帮助。
这应该为您提供一个三列数据框架,每个标签一行,运行总计的日期最接近,但是不低于0,金额(该日期总数 - 运行量)。
I believe a .cumsum() and .idxmin() will help with this.
This should give you a three-column data frame with one row per label, the date when the running total was closest to but not lower than 0, and the amount (Total - Running Quantity at that date).