使用np.哪里数据帧上有多个条件

2024-09-27 07:35:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为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   |

Tags: ltdateprodwellafter总和beforevolumes
2条回答

这是一个有点冗长,但可以作为一个不错的草案,你正在努力实现。它假设日期可以比较(因此它们存储为datetime而不是字符串)。在

condition = volumes['Date'] <= volumes['BeforeDate']

# Before
volumes.loc[(condition) & (volumes['Before'] == 'Prod'),
            'totals_before'] = volumes['Oil'] + volumes['Water']
volumes.loc[(condition) & (volumes['Before'] == 'Inj'),
            'totals_before'] = volumes['Inject']

# After
volumes.loc[(~condition) & (volumes['Before'] == 'Prod'),
            'totals_after'] = volumes['Oil'] + volumes['Water']
volumes.loc[(~condition) & (volumes['Before'] == 'Inj'),
            'totals_after'] = volumes['Inject']

volumes = volumes.sort_values(by=['WellName', 'Date'])
volumes['totals_before'] = volumes['totals_before'].fillna(method='ffill')

volumes['new_Tots'] = volumes['totals_before'] + volumes['totals_after'].fillna(0)

哪些输出:

^{pr2}$

如果以下假设是正确的,这一点可以大大简化:当注入填充时,油柱和水柱始终为空,反之亦然。在

我觉得这应该行得通。在

Prod_part = volumes.where(volumes.Date <= volumes.BeforeDate)\
                   .where(volumes.Before == "Prod")[["Water", "Oil"]].sum(
                          axis=1, min_count=1)
Inj_part = volumes.where(volumes.Date <= volumes.BeforeDate).where(volumes.Before == "Inj")["Inject"]

volumes["totals_before"] = Inj_part.combine_first(Prod_part)
volumes.totals_before.ffill(inplace=True)


0      20.0
1      30.0
2      30.0
3     700.0
4     720.0
5     720.0
6    1000.0
7    1500.0
8    1500.0

同样,使用to_dict函数给你的数据帧来拯救我们的生命。在

相关问题 更多 >

    热门问题