使用map对数据框执行操作并减少

问题描述

我有一个数据框,其中包含每天由不同商店出售的商品:

         date  date_block_num  shop_id  item_id  item_price  item_cnt_day
0  02.01.2013               0       59    22154      999.00           1.0
1  03.01.2013               0       25     2552      899.00           1.0
2  05.01.2013               0       25     2552      899.00          -1.0
3  06.01.2013               0       25     2554     1709.05           1.0
4  15.01.2013               0       25     2555     1099.00           1.0

我想使用shop_id并通过过滤,映射和归约来获取每个指定商店的结果。

所以我尝试了:

each_shop = filter(lambda n: n==transactions.shop_id,transactions)
results = map(lambda n: transactions.item_price*transactions.item_cnt_day,each_shop)

for result in results:
    print(result)

但是得到了:

/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py:798: FutureWarning: elementwise comparison Failed; returning scalar instead,but in the future will perform elementwise comparison
  result = getattr(x,name)(y)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-4e86d47b9f0d> in <module>()
      3 each_shop = filter(lambda n: n==transactions.shop_id,transactions)
      4 results = map(lambda n: transactions.item_price*transactions.item_cnt_day,each_shop)
----> 5 for result in results:
      6     print(result)
      7 

<ipython-input-21-4e86d47b9f0d> in <lambda>(n)
      1 # YOUR CODE GOES HERE
      2 # map
----> 3 each_shop = filter(lambda n: n==transactions.shop_id,each_shop)
      5 for result in results:

/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py in wrapper(self,other,axis)
    859 
    860             with np.errstate(all='ignore'):
--> 861                 res = na_op(values,other)
    862             if is_scalar(res):
    863                 raise TypeError('Could not compare %s type with Series' %

/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py in na_op(x,y)
    798                     result = getattr(x,name)(y)
    799                 if result is NotImplemented:
--> 800                     raise TypeError("invalid type comparison")
    801             except AttributeError:
    802                 result = op(x,y)

TypeError: invalid type comparison

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)