Perform operations on a dataframe with map and reduce

434 Views Asked by At

I have a dataframe with the items sold by different stores every day:

         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

I would like to get the results for each designated store with shop_id with filter, map and reduce.

So I tried:

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)

But got:

/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, transactions)
      4 results = map(lambda n: transactions.item_price*transactions.item_cnt_day, 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
0

There are 0 best solutions below