Function f(x,y) that takes two Pandas Series and returns a floating point number. I would like to apply f to each pair of columns in a DataFrame D and construct another DataFrame E of the returned values, so that f(D[i],D[j]) is the value of the ith row and jth column. The straightforward solution is to run a nested loop over all pairs of columns:
E = pd.DataFrame([[f(D[i], D[j]) for i in D] for j in D],
columns=D.columns, index=D.columns)
But is there a more elegant solution that perhaps would not involve explicit loops?
NB This question is not a dupe of this, despite the similar names.
EDIT A toy example:
D = pd.DataFrame([[1,2,3], [4,5,6], [7,8,9]], columns=("a","b","c"))
def f(x,y): return x.dot(y)
E
# a b c
#a 66 78 90
#b 78 93 108
#c 90 108 126