Calculate Different Types of Spend - Pandas/Numpy - Python
<p>I have 2 dataframes :</p>

<pre><code>df1
+------------+-------------+------+
| Product ID | Cost Method | Rate |
+------------+-------------+------+
|         10 | CPM         | 10   |
|         20 | CPC         | 0.3  |
|         30 | CPCV        | 0.4  |
|         40 | FLF         | 100  |
|         50 | VAD         | 0    |
|         60 | CPM         | 0.1  |
+------------+-------------+------+

df2
+--------+------------+-------------+--------+-----------------+
|  Date  | Product ID | Impressions | Clicks | Completed Views |
+--------+------------+-------------+--------+-----------------+
| 01-Jan |         10 |         300 |      4 |               0 |
| 02-Jan |         20 |          30 |      3 |               0 |
| 03-Jan |         30 |         200 |      4 |              20 |
| 02-Jan |         40 |         300 |      4 |               0 |
| 02-Jan |         40 |         500 |      4 |               0 |
| 03-Jan |         40 |         200 |      3 |               0 |
| 04-Jan |         90 |        3000 |      3 |               0 |
| 05-Jan |         50 |        3000 |      5 |               0 |
+--------+------------+-------------+--------+-----------------+
</code></pre>

<p>The ideal output is this:</p>

<pre><code> +--------+------------+-------------+--------+-----------------+--------+
|  Date  | Product ID | Impressions | Clicks | Completed Views | Spend  |
+--------+------------+-------------+--------+-----------------+--------+
| 01-Jan |         10 |         300 |      4 |               0 |  $3    |
| 02-Jan |         20 |          30 |      3 |               0 |  $1    |
| 03-Jan |         30 |         200 |      4 |              20 |  $8    |
| 02-Jan |         40 |         300 |      4 |               0 |  $50   |
| 02-Jan |         40 |         500 |      4 |               0 |  $50   |
| 03-Jan |         40 |         200 |      3 |               0 |  $-    |
| 04-Jan |         90 |        3000 |      3 |               0 |  $-    |
| 05-Jan |         50 |        3000 |      5 |               0 |  $-    |
+--------+------------+-------------+--------+-----------------+--------+
</code></pre>

<p>Where : </p>

<ol>
<li>Product is Matched by its ID In case an ID can't be Matched, then
the product spend is calculated at 0 </li>
<li>Where FLF is calculated as the
sum of total impressions for that product per day, and if that sums
is over a certain minimum limit, e.g. 600 impressions, then the rate
is applied. If there are two or more entries for the same day, then
the rate is divided equally by the count of times it appears in the
same day </li>
<li>Where, if a product is VAD, then the spend is 0 </li>
<li>Where CPC is calculated as the rate times the number of clicks</li>
<li>Where CPM is calculated as rate*(impression / 1000)</li>
</ol>