I have two pandas DataFrames / Series containing one row each.
df1 = pd.DataFrame([1, 2, 3, 4])
df2 = pd.DataFrame(['one', 'two', 'three', 'four'])
I now want to get all possible combinations into an n*n matrix / DataFrame with values for all cross-products being the output from a custom function.
def my_function(x, y):
return f"{x}:{y}"
This should therefore result in:
df = pd.DataFrame([['1:one', '2:one', '3:one', '4:one'],
['1:two', '2:two', '3:two', '4:two'],
['1:three', '2:three', '3:three', '4:three'],
['1:four', '2:four', '3:four', '4:four']])
0 1 2 3
0 1:one 2:one 3:one 4:one
1 1:two 2:two 3:two 4:two
2 1:three 2:three 3:three 4:three
3 1:four 2:four 3:four 4:four
While I can build my own matrix through itertools.product, this seems like a very inefficient way for larger datasets and I was wondering if there is a more pythonic way. Thank you in advance.