2016-11-10 43 views
1

我有2个dataframes:计算支出的不同类型 - 熊猫/ numpy的 - Python的

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 | 
+--------+------------+-------------+--------+-----------------+ 

理想的输出是这样的:

+--------+------------+-------------+--------+-----------------+--------+ 
| 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 | $- | 
+--------+------------+-------------+--------+-----------------+--------+ 

其中:

  1. 产品匹配通过其ID如果ID不能匹配,则 产品支出计算在0
  2. 其中FLF计算为该产品每天总展示次数的总和 ,并且如果该总和 超过了某个最低限制,例如, 600次展示,则应用价格 。如果有在同一天两个或多个条目,然后 速率同样受到次计数它出现在 同日,分
  3. 的地方,如果一个产品是VAD,那么支出为0
  4. 当中共作为速率倍计算,其中CPM作为率*(曝光/ 1000)
+1

嗨,我不是故意粗鲁,但这不是一项家庭作业服务。你尝试过什么吗?你遇到什么特定的路障 –

+0

嗨朱利安,绝对!最大的问题是确保FLF是按当天的总金额计算的,然后价值按发生的时间分割 –

回答

2

计算点击

  • 的数量我要去,即使我真的不应该回答你。你是堆栈溢出(SO)的新手,所以让这是一个教育帖子。放心,这篇文章的语气并不是想要居高临下或苛刻。


    首先,要问合适的问题(阅读this请),你需要做两件事情:

    • 说明你已经尝试过什么,说明你的问题是什么(提供了一个代码示例!) 。目前格式的问题肯定不符合要求。其中有5或6种完全不同的东西,感觉就像你只是要求某人做你的功课。
    • 提供一个可行的例子。

    对于可行的例子,你有这样做,但你选择的格式真的很烦人,因为不能直接使用pd.read_clipboard()加载数据。这里的人是志愿者他们的时间,如果他们必须花5或10分钟重新创建您的数据,他们可能不会这样做。

    这是我会怎么做它:

    这是第一个数据帧,使用df1 = pd.read_clipboard(index_col=0)加载它:

    ProductID  CostMethod Rate 
    
    10    CPM 10.0 
    20    CPC 0.3 
    30    CPCV 0.4 
    40    FLF 100.0 
    50    VAD 0.0 
    60    CPM 0.1 
    

    这里是第二个数据帧,使用df2 = pd.read_clipboard(index_col=0)加载它:

    ProductID Date Impressions Clicks CompletedViews 
    10   01-Jan   300  4    0 
    20   02-Jan   30  3    0 
    30   03-Jan   200  4    20 
    40   02-Jan   300  4    0 
    40   02-Jan   500  4    0 
    40   03-Jan   200  3    0 
    90   04-Jan   3000  3    0 
    50   05-Jan   3000  5    0 
    

    现在,只要做你的功课,这里有一个关于ed解决方案。我相信你会试着理解这段代码的作用,而不是重复使用它。

    第1步:合并双方dataframes

    我合并留在DF2,这是非常重要的。了解更多熊猫文档上Merging

    df3 = df2.merge(df1, left_index=True, right_index=True, how='left') 
    df3 
    

    Merged df3

    第2步:计算你的支出

    我们要编写自定义函数,然后做dataframe.apply

    def calc_spend(row): 
        """ 
        Accepts a row of the dataframe (df3.apply(calc_spend, axis=1)), 
        and computes the spend according to these rules: 
        * If costMethod is NaN, then zero 
        * 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 
        * Where, if a product is VAD, then the spend is 0 
        * Where CPC is calculated as the rate times the number of clicks 
        * Where CPM is calculated as rate*(impression/1000) 
        """ 
    
        if row.CostMethod == 'FLF': 
         # Calc the sum of total impressions for that product 
         # I'm using boolean indexing to select the rows where both productID and Date 
         # are the same as the current row 
         filterdateproductid = (df3.Date == row.Date) & (df3.index == row.name) 
         total_impressions = df3.ix[filterdateproductid, 'Impressions'].sum() 
         if total_impressions < 600: 
          spend = total_impressions 
         else: 
          count = df3.ix[filterdateproductid].shape[0] 
          rate = row.Rate/count # If you use python 2.7 make sure you do "from future import division" 
          spend = rate * total_impressions/1000.0 
    
        elif row.CostMethod == 'VAD': 
         spend = 0 
    
        elif row.CostMethod == 'CPC': 
         spend = row.Rate * row.Clicks 
    
        elif row.CostMethod == 'CPM': 
         spend = row.Rate * row.Impressions/1000.0 
    
        else: # Includes the case where the costMethod is Na 
         spend = 0 
    
        return spend 
    

    现在我们可以应用这个函数本身:

    df3['Spend'] = df3.apply(calc_spend, axis=1) 
    df3 
    

    Final result

    你也许注意到,“花”我计算是不完全一样的你,但这是因为你对如何计算的初始规格没有那么大。您可以轻松更改calc_spend功能以符合您的要求。

  • +1

    嗨Julien, 非常感谢您的帮助。本周我开始使用Python(来自excel),并且我正在尝试将我的工作文件从excel迁移到python(随着学习的进行)。通常我设法让事情继续下去。但在这里我真的很难过。我非常感谢你回答这个问题的时间。对我来说意义重大!正如您所建议的那样,下次我将确保更好地设置我的问题 –