Suppose I have 10 columns and I would like to divide each column with the the previous column using apply and lambda. This is my code
df.apply(lambda x: df.iloc[:,x+1]/df.iloc[:,x])
But got the error message
positional indexers are out-of-bounds
I tried to use this
df.apply(lambda x: df.iloc[:,x+1]/df.iloc[:,x] if x < df.shape[1] else np.nan, axis = 1, result_type='expand')
But gives different error messages
The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().