Skip to content Skip to sidebar Skip to footer

Pandas Apply Valueerror: The Truth Value Of A Series Is Ambigous

I'm trying to create a new feature using df_transactions['emome'] = df_transactions['emome'].apply(lambda x: 1 if df_transactions['plan_list_price'] ==0 & df_transactions['actu

Solution 1:

You are really close, but much better is vectorized solution without apply - get boolean mask and convert to int:

mask = (df_transactions['plan_list_price'] == 0) & 
       (df_transactions['actual_amount_paid'] > 0)
df_transactions['emome'] = mask.astype(int)

If really want slowier apply:

f = lambda x: 1if x['plan_list_price'] ==0and x['actual_amount_paid'] > 0else0
df_transactions['emome'] = df_transactions.apply(f, axis=1)

Sample:

df_transactions = pd.DataFrame({'A':list('abcdef'),
                                'plan_list_price':[0,0,0,5,5,0],
                                'actual_amount_paid':[-1,0,9,4,2,3]})


mask = (df_transactions['plan_list_price'] == 0) & \
       (df_transactions['actual_amount_paid'] > 0)
df_transactions['emome1'] = mask.astype(int)

f = lambda x: 1if x['plan_list_price'] ==0and x['actual_amount_paid'] > 0else0
df_transactions['emome2'] = df_transactions.apply(f, axis=1)
print (df_transactions)

   A  actual_amount_paid  plan_list_price  emome1  emome2
0  a                  -10001  b                   00002  c                   90113  d                   45004  e                   25005  f                   3011

Timings:

#[60000rows]
df_transactions = pd.concat([df_transactions] *10000, ignore_index=True)

In [201]: %timeit df_transactions['emome1'] = ((df_transactions['plan_list_price'] ==0) & (df_transactions['actual_amount_paid'] >0)).astype(int)
1000 loops, best of3: 971 µs per loop

In [202]: %timeit df_transactions['emome2'] = df_transactions.apply(lambda x: 1 if x['plan_list_price'] ==0and x['actual_amount_paid'] >0else0, axis=1)
1 loop, best of3: 1.15 s per loop

Solution 2:

A few issues:

  • On the right side of the equation, the new field (emome)is not created yet.
  • The lambda function is on x, not on df_transactions, which does not exist in this scope.
  • You need to specify axis since you are applying to each row (default is to each column).

From Doc:

axis : {0 or ‘index’, 1 or ‘columns’}, default 0 Axis along which the function is applied:

0 or ‘index’: apply function to each column. 1 or ‘columns’: apply function to each row.

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.apply.html

Post a Comment for "Pandas Apply Valueerror: The Truth Value Of A Series Is Ambigous"