I am trying to run the following code multiple times. Each time I run it I get different results because of the df.sample function. The output is a dataframe with 2 rows by 750 columns of continuous data because of the calculation loops seen near the end. How do I turn this into a loop so that I can run this code 100 times and save the dataframes output together? Any help would be very much appreciated! Thank you
df1 = dfs.sample(n=len(df_sample[df_sample.Column_name== 'Category']),replace=True)
df2 = dfs2.sample(n=len(df_sample[df_sample.Column_name== 'Category']),replace=True)
df3 = dfs3.sample(n=len(df_sample[df_sample.Column_name== 'Category']),replace=True)
all_dfs = [df1, df2, df3]
df4 = pd.concat(all_dfs, ignore_index=True)
#shp_gpd is a geopandas dataframe
#creates a column to merge df with geodataframe
df4['tmp'] = np.arange(len(df3))
shp_gpd['tmp'] = np.arange(len(shp_gpd))
df = df_gpd.merge(df3, on = "tmp", how='left')
gdf = gpd.GeoDataFrame(df)
#Calculations
collist = list(gdf.columns)
emptydict = []
for j in collist[14:]:
B1 = (((gdf.groupby(['Category1'])['Category2'].sum())/(gdf['Category2'].sum())) *
(gdf.groupby(['Category1'])[j].mean())).sum()
res = {j:B1}
emptydict.append(res)
resdf = pd.DataFrame(emptydict)
resdf1 = resdf.apply(lambda x: pd.Series(x.dropna().values))
resdf1
#standard deviation by for previous equation
collist1 = list(gdf.columns)
emptydict1 = []
for k in collist1[14:]:
SD = (((gdf.groupby(['Category1'])['Category2'].sum())/(gdf['Category2'].sum())) *
(gdf.groupby(['Category1'])[k].std())).sum()
res = {k:SD}
emptydict1.append(res)
SDdf = pd.DataFrame(emptydict1)
SDdf1 = SDdf.apply(lambda x: pd.Series(x.dropna().values))
df5 = [resdf1, SDdf1]
df6 = pd.concat(df5)
df6
#print(df6)