Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 16743

Lambda in Apply

$
0
0

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().

Viewing all articles
Browse latest Browse all 16743

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>