熊猫:计算订单日期之间的消费率

时间:2020-09-19 13:25:30

标签: python pandas

我的熊猫数据框如下:

| House | CheckDate | Reading | OrderDate | OrderedQuantity | DeliveryDate | DeliveredQuantity |
------------------------------------------------------------------------------------------------
|   A   | 2020-01-01|  43.21  |    NaT    |      NaN        |    NaT       |        NaN        |
|   A   | 2020-01-02|  43.06  |    NaT    |      NaN        |    NaT       |        NaN        |
|   A   | 2020-01-03|  42.97  |    NaT    |      NaN        |    NaT       |        NaN        |
---
|   A   | 2020-05-01|  9.82   | 2020-05-01|      50         |    NaT       |        NaN        |
|   A   | 2020-05-02|  9.65   |    NaT    |      NaN        |    NaT       |        NaN        |
---
|   A   | 2020-05-15|  0.23   |    NaT    |      NaN        |    NaT       |        NaN        |
|   A   | 2020-05-15|  25.94  |    NaT    |      NaN        |  2020-05-15  |        50         |
|   A   | 2020-05-16|  49.71  |    NaT    |      NaN        |    NaT       |        NaN        |
---
|   A   | 2020-06-17|  6.57   | 2020-06-17|      50         |    NaT       |        NaN        |
---
|   A   | 2020-07-03|  9.65   |    NaT    |      NaN        |    NaT       |        NaN        |

每天为每所房屋读取一次,这是该天剩余的某种物料的数量。 CheckDate是读取数据的日期。我正在尝试计算订购日期之间房屋消耗的材料量。对于这个特殊的房子,总共有4个订单和4个交货。交货可能发生在两个订单位置之间,因此每次交货时读取值都会增加。

我需要计算订购日期之间每天的材料消耗。这不仅仅只是读取第一个订购日期和最后一个订购日期之间的差异。

为进行数学解释,第一个与最后一个订购日期之间的总消耗量等于(所有交货数量的总和+第一个订购日期的读数-最后一个订购日期的读数)。将其除以第一个和最后一个订购日期之间的总天数,便得出每天的消费量。

我是Python的新手。这就是我在SQL中的处理方式。

SELECT SUM(Reading) As Sum_Of_Del_Quant FROM table
WHERE House = A AND DeliveryDate != "NaT";

SELECT DATEDIFF(MAX(OrderDate), MIN(OrderDate) AS Number_of_days FROM table
WHERE House = A;

SELECT Reading AS Value_on_First_Order_Date FROM table 
WHERE House = A AND OrderDate=(SELECT MIN(OrderDate) FROM table WHERE House = A); 

SELECT Reading AS Value_on_Last_Order_Date FROM table 
WHERE House = A AND OrderDate=(SELECT MAX(OrderDate) FROM table WHERE House = A);

有人可以解释如何在Python中做到这一点吗?

请参阅下面有关如何进行数学计算的评论。出于某些奇怪的原因,我不允许在我的帖子中包含该信息。

0 个答案:

没有答案