Replace this for loop with a pandas vector operation I am running this for loop in python to create a list of differences of means of two dataframes, and then using that list to generate a random values under a normal distribution.
df_means = []

for _ in range(10000):
    df_sample = df_common.sample(1000, replace=True)
    d_mean = df_sample[df_sample.doggo == True].retweet_count.mean()
    nd_mean = df_sample[df_sample.doggo == False].retweet_count.mean()
    df_means.append(d_mean - nd_mean)
    
# under null hypothesis mean will be 0    
nulls = np.random.normal(0,np.std(df_means), 10000)    

However, given that it is a for loop, this is taking a long time to run. Is there a way I can replace the for loop with a vector operation to speed up code execution?
Try using list comprehension:
df_means = [np.diff(x)[0] for x in [df_common.sample(1000, replace = True).groupby([&quot;doggo&quot;]).retweet_count.mean().values.tolist() for _ in range(10000)]