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

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

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

  1. # now calculate the % Change in Quantity and % Change in Price
  2. # % Change in Quantity = Q2-Q1/(Q2+Q1)/2 *100
  3. # % Change in Price = Q2-Q1/(Q2+Q1)/2 *100
  4. # PED = % Change in Quantity/% Change in Price
  5. # create unique lists for Geography and Product and week ending:
  6. geo_list = data2['Geography'].drop_duplicates().tolist()
  7. product_list = data2['Product'].drop_duplicates().tolist()
  8. date_list = data2['Week Ending'].drop_duplicates().tolist()
  9. # create empty List for Price Elasticity of Demand:
  10. PED_list = []
  11. for d in date_list:
  12. for g in geo_list:
  13. [![enter image description here][1]][1]for p in product_list:
  14. # get log_price for every product in the product list within every geography in the
  15. geography list
  16. log_price = data2.loc[(data2["Week Ending"]==d) & (data2["Geography"]==g) &
  17. (data2["Product"]==p)]["log_price"]
  18. # get qty for every product in the product list within every geography in the geography list
  19. qty = log_price = data2.loc[(data2["Week Ending"]==d) & (data2["Geography"]==g) &
  20. (data2["Product"]==p)]["norm_qty"]
  21. ###### This is where we lose data #####
  22. # calculate price numerator Q2 - Q1
  23. price_num = data2.loc[(data2["Week Ending"]==d) & (data2["Geography"]==g) &
  24. (data2["Product"]==p)]["log_price"].diff(1).values
  25. # calculate price denominator (Q2 + Q1)/2
  26. price_den = np.divide(log_price.cumsum(),2)
  27. # calculate % Change in Price:
  28. Delta_Price = np.divide(price_num, price_den)*100
  29. # calculate demand numerator Q2 - Q1:
  30. demand_num = qty.diff(1).values
  31. # calculate demand denominator: (Q2 + Q1)/2
  32. demand_den = np.divide(qty.cumsum(),2)
  33. # calculate % Change in Quantity Demand:
  34. Delta_Demand = np.divide(demand_num, demand_den)
  35. # Calculate Price Elasticity of Demand:
  36. PED = np.divide(Delta_Price, Delta_Demand)```
  37. [1]: https://i.stack.imgur.com/zdoAO.png

暂无答案!

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

相关问题