0

I tried to create a new column called brought_profit_to_company, by filtering other two columns product_id and refunded, however it has resulted in some errors:

df['brought_profit_to_company'] = 10
if (df['product_id'] == 7.99 & df['refunded'] == True):
   df['brought_profit_to_company'] = 0
elif (df['product_id'] == '49.99'):
   df['brought_profit_to_company'] == 49.99
else:
   df['brought_profit_to_company'] = df['product_id'] * (df['days_used_app'] * 1)

but it has resulted in this error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/ops/array_ops.py in na_logical_op(x, y, op)
    301         #  (xint or xbool) and (yint or bool)
--> 302         result = op(x, y)
    303     except TypeError:

8 frames
TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
ValueError: Buffer dtype mismatch, expected 'Python object' but got 'bool'

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/ops/array_ops.py in na_logical_op(x, y, op)
    326                     f"Cannot perform '{op.__name__}' with a dtyped [{x.dtype}] array "
    327                     f"and scalar of type [{typ}]"
--> 328                 ) from err
    329 
    330     return result.reshape(x.shape)

TypeError: Cannot perform 'rand_' with a dtyped [bool] array and scalar of type [bool]

Here is also dtypes of columns

user_id                      int64
product_id                 float64
trial                         bool
refunded                      bool
purchase_date_x     datetime64[ns]
country_code                object
media_source                object
days_used_app               int64
dtype: object
  • 1
    Does this answer your question? [Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()](https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o) – Ynjxsjmh May 25 '22 at 16:21

1 Answers1

0

First, the & operator in python is bitwise, I guess you are going for logical and. Second, I would recommend using the apply method

df['brought_profit_to_company'] =  df.apply(lambda col: 0 if (col['product_id'] == 7.99 and col['refund']) else 49.99 if (col['product_id'] == 49.99 ) else col['product_id'] * (col['days_used_app'] * 1), axis=1)

cheers ✌

shai gindin
  • 109
  • 3