我有一个名为volumes的数据框,其中包含数千口井的日期和编号。
| WellName | Date | Oil | Water | Inject |BeforeDate| Before | After | AfterDate
|----------|----------|-----|-------|--------|--------- |--------|-------|----------
| Well_1 | 1/1/2000 | 10 | 10 | | 1/1/2001 | Prod | Inj | 1/1/2002
| Well_1 | 1/1/2001 | 10 | 20 | | 1/1/2001 | Prod | Inj | 1/1/2002
| Well_1 | 1/1/2002 | 50 | 60 | | 1/1/2001 | Prod | Inj | 1/1/2002
| Well_2 | 1/1/2000 | | | 700 | 1/1/2001 | Inj | Prod | 1/1/2002
| Well_2 | 1/1/2001 | | | 720 | 1/1/2001 | Inj | Prod | 1/1/2002
| Well_2 | 1/1/2002 | | | 800 | 1/1/2001 | Inj | Prod | 1/1/2002
| Well_3 | 1/1/2000 | | | 1000 | 1/1/2001 | Inj | Inj | 1/1/2002
| Well_3 | 1/1/2001 | | | 1500 | 1/1/2001 | Inj | Inj | 1/1/2002
| Well_3 | 1/1/2002 | | | 2000 | 1/1/2001 | Inj | Inj | 1/1/2002
对于日期为<;=BeforeDate&BeforeDate==“Prod”的油井,我需要按年份求和油+水柱的总和,否则我要求注油列的总和,其中Date<;=BeforeDate&Before==“Inj”。在
如何包括Else Date<;=BeforeDate&Before==Inj'?在
这就是我到目前为止所得到的,并意识到这是不正确的。在
^{pr2}$一旦volumes['totals_before']计算正确,我将需要向前填充(ffill)最近的总和(在本例中为1/1/2001),并将其添加到另一列volumes['totals_after']中,即Date>;=AfterDate。
最终结果如下:
volumes['new_Tots'] = volumes['totals_before'] + volumes['totals_after']
预期输出:
| WellName | Date | totals_before | totals_after | new_Tots |
|----------|----------|---------------|--------------|----------|
| Well_1 | 1/1/2000 | 20 | | 20 |
| Well_1 | 1/1/2001 | 30 | | 30 |
| Well_1 | 1/1/2002 | 30(ffill) | 110 | 140 |
| Well_2 | 1/1/2000 | 700 | | 700 |
| Well_2 | 1/1/2001 | 720 | | 720 |
| Well_2 | 1/1/2002 | 720(ffill) | 800 | 1520 |
| Well_3 | 1/1/2000 | 1000 | | 1000 |
| Well_3 | 1/1/2001 | 1500 | | 1500 |
| Well_3 | 1/1/2002 | 1500(ffill) | 2000 | 3500 |
这是一个有点冗长,但可以作为一个不错的草案,你正在努力实现。它假设日期可以比较(因此它们存储为
datetime
而不是字符串)。在哪些输出:
^{pr2}$如果以下假设是正确的,这一点可以大大简化:当注入填充时,油柱和水柱始终为空,反之亦然。在
我觉得这应该行得通。在
同样,使用
to_dict
函数给你的数据帧来拯救我们的生命。在相关问题 更多 >
编程相关推荐