I am trying to understand how to make code works faster by converting for-loops to vectorized operations. As an example I use the following dataframe: