Using `itertools` to combine DataFrame columns

Solution 1:

You could generate the list a 2 elem combinations of adjacent columns with wraparound using this list comprehension Then you could generate additional columns by iterating through this list

comb = [(x, df.columns[(i+1) % len(df.columns)]) for i, x in enumerate(df.columns)]


for x, y in comb:
  df[f'{x}_{y}'] = df[x] - df[y]

this produces the output:

        u       g       r       i    zmag      W1      W2     NUV     FUV    u_g    g_r    r_i  i_zmag  zmag_W1  W1_W2  W2_NUV  NUV_FUV  FUV_u
0  20.078  19.679  19.585  19.406  19.370  14.970  13.992  20.122  20.736  0.399  0.094  0.179   0.036    4.400  0.978  -6.130   -0.614  0.658
1  20.443  19.115  18.918  18.749  18.698  14.638  14.041  21.646  21.456  1.328  0.197  0.169   0.051    4.060  0.597  -7.605    0.190  1.013
2  19.723  19.593  19.353  19.175  19.258  15.193  14.354  21.122  21.090  0.130  0.240  0.178  -0.083    4.065  0.839  -6.768    0.032  1.367
3  19.683  19.393  19.273  18.995  18.950  15.545  14.530  22.465  20.091  0.290  0.120  0.278   0.045    3.405  1.015  -7.935    2.374  0.408
4  19.769  19.233  19.083  18.983  18.768  14.978  14.224  21.684  20.314  0.536  0.150  0.100   0.215    3.790  0.754  -7.460    1.370  0.545
5  19.908  19.500  19.065  18.838  18.354  13.837  13.016  21.307  21.234  0.408  0.435  0.227   0.484    4.517  0.821  -8.291    0.073  1.326