python中计算需求价格弹性的三个for循环正在返回nan值

332nm8kg  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(241)

我试图计算需求的价格弹性,然后将这些值返回到我的数据框架中。我需要创建三个for循环,因为计算需要考虑每个惟一的日期、惟一的地理位置和惟一的产品来计算ped。我的循环将运行,但一旦到达“price\ num”代码行,它将返回空值列表。任何帮助都将不胜感激,谢谢!


# now calculate the % Change in Quantity and % Change in Price

    # % Change in Quantity = Q2-Q1/(Q2+Q1)/2 *100
    # % Change in Price = Q2-Q1/(Q2+Q1)/2 *100
    # PED = % Change in Quantity/% Change in Price

    # create unique lists for Geography and Product and week ending:
    geo_list = data2['Geography'].drop_duplicates().tolist()
    product_list = data2['Product'].drop_duplicates().tolist()
    date_list = data2['Week Ending'].drop_duplicates().tolist()

    # create empty List for Price Elasticity of Demand:
    PED_list = []

    for d in date_list:
       for g in geo_list:
           [![enter image description here][1]][1]for p in product_list:
              # get log_price for every product in the product list within every geography in the 
              geography list
              log_price = data2.loc[(data2["Week Ending"]==d) & (data2["Geography"]==g) & 
              (data2["Product"]==p)]["log_price"]

              # get qty for every product in the product list within every geography in the geography list
              qty = log_price  = data2.loc[(data2["Week Ending"]==d) & (data2["Geography"]==g) & 
              (data2["Product"]==p)]["norm_qty"]

              ###### This is where we lose data #####
              # calculate price numerator Q2 - Q1
              price_num = data2.loc[(data2["Week Ending"]==d) & (data2["Geography"]==g) & 
              (data2["Product"]==p)]["log_price"].diff(1).values

              # calculate price denominator (Q2 + Q1)/2
              price_den = np.divide(log_price.cumsum(),2)

              # calculate % Change in Price:
              Delta_Price = np.divide(price_num, price_den)*100

              # calculate demand numerator Q2 - Q1:
              demand_num = qty.diff(1).values

              # calculate demand denominator: (Q2 + Q1)/2
              demand_den = np.divide(qty.cumsum(),2)

              # calculate % Change in Quantity Demand:
              Delta_Demand = np.divide(demand_num, demand_den)

              # Calculate Price Elasticity of Demand:
              PED = np.divide(Delta_Price, Delta_Demand)```

  [1]: https://i.stack.imgur.com/zdoAO.png

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题