Matrix of all possible multipliable outcomes from two lists into dataframe

Viewed 210

I have two lists and am trying to create a matrix of all possible multiplication outcomes in a dataframe using pandas.

Lists:

>>> L1
[8, 1, 4, 2, 7, 5]

>>> L2
[5, 3, 9, 1, 2, 6]

I have multiplied every item from L1 with every item from L2 as followed to formulate all possible outcomes:

>>> [[a*b] for a in L1 for b in L2]
[[40], [24], [72], [8], [16], [48], [5], [3], [9], [1], [2], [6], [20], [12], [36], [4], [8], [24], [10], [6], [18], [2], [4], [12], [35], [21], [63], [7], [14], [42], [25], [15], [45], [5], [10], [30]]

Expected Output/Goal:

My goal is to now represent these values as a matrix using pandas but I am not sure of where to start. The first item in the lists are assigned the column/row name from 0-5.

For example:

The df matrix should look something like:

   0  1  2  3  4  5
0  40  5 20 10 35 25
1  24  3 12  6 21 15
2  72  9 36 18 63 45
3  8   1  4  2  7 5
4  16  2  8  4 14 10
5  48  6 24 12 42 30
2 Answers
Related