Skip to content
Advertisement

Tag: vectorization

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. Answer This seems overkill. Your state variable basically is the previous value in df_A[‘a’]*10. So we can just use shift:

How to vectorize groupby and apply in pandas?

I’m trying to calculate (x-x.mean()) / (x.std +0.01) on several columns of a dataframe based on groups. My original dataframe is very large. Although I’ve splitted the original file into several chunks and I’m using multiprocessing to run the script on each chunk of the file, but still every chunk of the dataframe is very large and this process never

bootstrap numpy 2D array

I am trying to sample with replacement a base 2D numpy array with shape of (4,2) by rows, say 10 times. The final output should be a 3D numpy array. Have tried the code below, it works. But is there a way to do it without the for loop? Answer Here’s one vectorized approach – Basic idea is that we

Python pandas calculate rolling stock beta using rolling apply to groupby object in vectorized fashion

I have a large data frame, df, containing 4 columns: etc. I am attempting to calculate a common financial measure, known as beta, using a function, that takes two of the columns, ret_1m, the monthly stock_return, and ret_1m_mkt, the market 1 month return for the same period (period_id). I want to apply a function (calc_beta) to calculate the 12-month result

Vectorize over the rows of an array

I have an array X and I want to apply a function f to all the rows of X: Now, y should be array([15,30], ‘i’). Which method or slicing magic will implement rows in the most efficient way? Answer NumPy implements the concept of “action over a particular axis”. The general function is numpy.apply_along_axis(): (where sum can of course be

Advertisement