I would like to create a column, in this given example 'amount', within a Pandas dataframe 'df' where the value of each row is based on its previous rows as well as the value from another column 'id'. Example, if 'id' already has the value 30 assigned to it in the 'amount' column, then 0 else 30.
The expected outcome shown below:
id amount
a 30
b 30
a 0
a 0
c 30
a 0
c 0
b 0
b 0
a 0
a 0
I thought I could accomplish this through some combination of groupby and lambda, but sadly I've repeatedly hit a wall.
What I tried out was:
df['amount'] = df.apply(
lambda x: 30 if df.groupby('id')['amount'].cumsum()<30
else 0)
This gives me the following error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I apologize in advance if the solution is obvious, but unfortunately, I haven't been able to find anything so far that would solve this.
df['amount'] = np.where(df.groupby('id')['amount'].cumsum() < 30, 30, 0)