I am getting this error: The truth value of a Series is ambiguous
in my lambda function. I know that here is a very comprehensive explanation around this error but I don't think this relates to my issue:
Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
Basically I am trying to determine via lambda whether OpenBal
is the same from one month to the next within the same AccountID and give me a '1' if it is the same (e.g. for OpenBal=101 below). Obviously the first record should give me a NaN. (P.S. thanks @jdehesa for your answers in my other post).
This demonstrates my problem:
import pandas as pd
df = pd.DataFrame({'AccountID': [1,1,1,1,2,2,2,2,2],
'RefMonth': [1,2,3,4,1,2,3,4,5],
'OpenBal': [100,101,101,103,200,201,202,203,204]})
SameBal = df.groupby('AccountID').apply(lambda g: 1 if g['OpenBal'].diff() == 0 else 0)
df['SameBal'] = SameBal.sortlevel(1).values