简体   繁体   中英

Pandas initialize dataframe with np.array().T

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   |
+-----+-----+-----+

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM