'How to vectorize this function to iterate over a particular column in dataframe?

for choice in df["Num"].values:
    i=0
    delta=0.5
    z=[x for x in a["num2"] if choice-delta <= x <= choice+delta] #Selecting a subset from list of random numbers that lies within a range
    df["New Num"].iloc[i]=random.choice(z) #Selecting a random number from the subset to update the column
    i=i+1

Here df is a dataframe in which I want to iterate over a column named "Num". For each element in Num, I want to update it using random element from a specified bound from a dataframe named a. Note: My dataset contains 150k values

df["Num"]-Which needs to be updated

a["Num2"]-The random sample from which I have to update



Solution 1:[1]

Not sure of what you are expecting.

But as you are taking random items from a["num2"] you can first use a.sample(frac=1) to shuffle rows before entering the loop.

Then, in order to change values in df, I would recommend to define a function update() and call df["Num"].apply(update) to reach your result.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sam C.