Home > Mobile >  Alternative to nested loop
Alternative to nested loop

Time:12-31

Newbie to coding here so apologies in advance if the question isnt formed well or has been answered before. I had a task that I accomplished by using a nested For loop. I wanted to know if the same can be done without the loops and in a more python-like way. More specifically; I had to populate the below data frame

Index NFL NBA NHL MLB
NFL p val p val p val p val
NBA p val p val p val p val
NHL p val p val p val p val
MLB p val p val p val p val

Where p val is the p-value from the stats.ttest_rel() method of the 'W/L Ratio'. The columns to pass to the ttest come from 4 dataframes - one dataframe for each of the leagues above (which contains the said 'W/L Ratio' column and a region column. The region column is the common key between the dataframes. Example below;

Index region W/L Ratio
0 Boston 0.66
1 Denver 0.55

I wanted to pass to the ttest the W/L Ratio columns from combinations of sport such that a region had teams in both those sports.

I did this by executing the following code

`

sport = [merge_nfl,merge_nba,merge_nhl,merge_mlb] # our league dataframes in a list
sports = ['NFL', 'NBA', 'NHL', 'MLB']
p_values = pd.DataFrame({k:np.nan for k in sports}, index=sports)
for i in range(4):
    for j in range(4):
        merge_df_final = pd.merge(sport[i],sport[j],how='inner',left_on='Metropolitan area', right_on='Metropolitan area')
        p_values.iloc[i,j] = stats.ttest_rel(merge_df_final.iloc[:,1],merge_df_final.iloc[:,2])[1]

I want to know if i can get the same outcome with out the use of the nested loop.

Thank you

CodePudding user response:

You can get rid of the nested loop like this-

for i, j in zip(range(4), range(4)):
    print(i, j)

It has the same output as

for i in range(4):
    for j in range(4):
        print(i, j)

CodePudding user response:

You can use product module for this

from itertools import product

for i,j in product(range(4),range(4)):
    merge_df_final = pd.merge(sport[i],sport[j],how='inner',left_on='Metropolitan 
    area', right_on='Metropolitan area')
    p_values.iloc[i,j] = 
    stats.ttest_rel(merge_df_final.iloc[:,1],merge_df_final.iloc[:,2])[1]
  • Related