0

I came across Ken Wei's comment on this answer stating that using pandas cartesian_product() while initializing the dataframe with np.array().T is faster than itertools.product for combining elements of two lists.

I'm confused as to how it would be used. Given two lists:

l1 = ['A', 'B']

l2 = [1, 2]

How would you arrive at this dataframe using his cartesian_product() and np.array().T?

+-----+-----+-----+
|     | l1  | l2  |
+-----+-----+-----+
|  0  | A   | 1   |
+-----+-----+-----+
|  1  | A   | 2   |
+-----+-----+-----+
|  2  | B   | 1   |
+-----+-----+-----+
|  3  | B   | 2   |
+-----+-----+-----+
bdoubleu
  • 5,568
  • 2
  • 20
  • 53

1 Answers1

0

Which means, as stated, not unpacking but rather using np.array().T:

>>> pd.DataFrame(np.array(pd.core.reshape.util.cartesian_product([l1, l2])).T)
   0  1
0  A  1
1  A  2
2  B  1
3  B  2
Colonel Beauvel
  • 30,423
  • 11
  • 47
  • 87