I want to convert a data frame to the format I want by scanning each latitude and longitude in the for loop, but this process takes too long. Is there a way to make the following script faster, such as using multi threads or processing? Can you show me how?
p=0
for i in tqdm(df_wind_monthly["lat"]):
for j in df_wind_monthly["lon"]:
print("lat: " + str(i) + " lon: " + str(j))
for k in range(1948,2017):
rslt_df_wind = df_wind_monthly.loc[(df_wind_monthly['lat'] == i) \
& (df_wind_monthly['lon'] == j) \
& (df_wind_monthly['year'] == k)]; rslt_df_wind = rslt_df_wind.reset_index()
month_columns.loc[p,"lat"]=rslt_df_wind.loc[0,"lat"]
month_columns.loc[p,"lon"]=rslt_df_wind.loc[0,"lon"]
month_columns.loc[p,"years"]=rslt_df_wind.loc[0,"year"]
month_columns.loc[p,"wind_January"]=rslt_df_wind.loc[rslt_df_wind['month'].loc[lambda x: x=="January"].index.tolist()[0],"wind"]
month_columns.loc[p,"wind_February"]=rslt_df_wind.loc[rslt_df_wind['month'].loc[lambda x: x=="February"].index.tolist()[0],"wind"]
month_columns.loc[p,"wind_March"]=rslt_df_wind.loc[rslt_df_wind['month'].loc[lambda x: x=="March"].index.tolist()[0],"wind"]
month_columns.loc[p,"wind_April"]=rslt_df_wind.loc[rslt_df_wind['month'].loc[lambda x: x=="April"].index.tolist()[0],"wind"]
month_columns.loc[p,"wind_May"]=rslt_df_wind.loc[rslt_df_wind['month'].loc[lambda x: x=="May"].index.tolist()[0],"wind"]
month_columns.loc[p,"wind_June"]=rslt_df_wind.loc[rslt_df_wind['month'].loc[lambda x: x=="June"].index.tolist()[0],"wind"]
month_columns.loc[p,"wind_July"]=rslt_df_wind.loc[rslt_df_wind['month'].loc[lambda x: x=="July"].index.tolist()[0],"wind"]
month_columns.loc[p,"wind_August"]=rslt_df_wind.loc[rslt_df_wind['month'].loc[lambda x: x=="August"].index.tolist()[0],"wind"]
month_columns.loc[p,"wind_September"]=rslt_df_wind.loc[rslt_df_wind['month'].loc[lambda x: x=="September"].index.tolist()[0],"wind"]
month_columns.loc[p,"wind_October"]=rslt_df_wind.loc[rslt_df_wind['month'].loc[lambda x: x=="October"].index.tolist()[0],"wind"]
month_columns.loc[p,"wind_November"]=rslt_df_wind.loc[rslt_df_wind['month'].loc[lambda x: x=="November"].index.tolist()[0],"wind"]
month_columns.loc[p,"wind_December"]=rslt_df_wind.loc[rslt_df_wind['month'].loc[lambda x: x=="December"].index.tolist()[0],"wind"]
p+=1
These are the inputs and expected output samples:
#df_wind_monthly (INPUT):
time lat lon wind
0 1948-01-16 15.125 15.125 6.509021
1 1948-01-16 15.125 15.375 6.485108
2 1948-01-16 15.125 15.625 6.472615
3 1948-01-16 15.125 15.875 6.472596
4 1948-01-16 15.125 16.125 6.486597
#Expected dataframe columns (OUTPUT):
month_columns=pd.DataFrame(columns=['lat',"lon","years","wind_January","wind_February","wind_March","wind_April","wind_May","wind_June","wind_July","wind_August","wind_September","wind_October","wind_November","wind_December"])