Skip to content
Advertisement

Python dataframe vectorizing for loop

I would like to vectorize this piece of python code with for loop conditioned on current state for speed and efficiency.

values for df_B are computed based on current-state (state) AND corresponding df_A value.

Any ideas would be appreciated.

JavaScript

Advertisement

Answer

This seems overkill. Your state variable basically is the previous value in df_A['a']*10. So we can just use shift:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement