0

How can I execute a nested loop that goes through two data frames to acquire every combination of the columns within the nested loop. This is for acquiring the various r^2 values across the various combinations utilizing OLS Regression

For example: DF1 consists of:

    50      51      52      53
0   73.44   63.44   53.46   44.49
1   395.01  369.01  343.01  317.49
2   339.75  312.76  286.8   262.8

DF2 consists of:

    50      51      52      53
0   153.81  173.81  193.83  214.86
1   19.98   23.98   27.98   32.46
2   3.06    5.07    8.11    13.11

How can I get every single combination of both data frame columns and execute the

Here is my code below:

g=50
h=53
for g in range(50, 53):

    q =[ round(elem, 2) for elem in DF1[g].iloc[0:].tolist() ]

    for h in range(50, 53):

        z =[ round(elem, 2) for elem in DF2[h].iloc[0:].tolist() ]
        x = np.row_stack((q,z))
        x = np.array(x).T
        x = sm.add_constant(x)
        results = sm.OLS(endog=y, exog=x).fit()
        my_list.append(results.rsquared)
        h += 1
    g+=1

For some reason I am not getting all the combinations of the columns? Any Suggestions?

2
  • what do you mean by getting all combinations of columns ? Commented Oct 26, 2015 at 13:12
  • so like column 50 of df1 then column 50 of df1, next column 50 of df1 and column 51 of df2, and so on until I get every combination. As the loop is processing, it is turning the columns into a list and going regressional analysis Commented Oct 26, 2015 at 13:16

1 Answer 1

1

You should specify range(50, 54) or use iteritems. This should show 4x4=16 outputs in total.

df1 = pd.DataFrame({50: [73.44, 395.01, 339.75],
                    51: [63.44, 369.01, 312.76],
                    52: [53.46, 343.01, 286.8],
                    53: [44.49, 317.49, 262.8]})
df2 = pd.DataFrame({50: [153.81, 19.98, 3.06],
                    51: [173.81, 27.98, 5.07],
                    52: [193.83, 27.98, 8.11],
                    53: [214.86, 32.46, 13.11]})

for _, g in df1.iteritems():
    for _, h in df2.iteritems():
        print(np.row_stack((g, h)))
# [[  73.44  395.01  339.75]
#  [ 153.81   19.98    3.06]]
# ...
# [[  44.49  317.49  262.8 ]
#  [ 214.86   32.46   13.11]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.