Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a pandas DataFrame from a Cartesian product of two large lists

I'm looking for the simplest way to create a data frame from two others such that it contains all combinations of their elements. For instance we have these two dataframes:

list1 = ["A", "B", "C", "D", "E"]
list2 = ["x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8"]

df1 = pd.DataFrame(list1)
df2 = pd.DataFrame(list2)

The result must be:

   0   1
0  A  x1
1  A  x2
2  A  x3
3  A  x4
4  A  x5
5  A  x6
6  A  x7
7  A  x8
8  B  x1
9  B  x2

I tried to combine from the lists and it works fine with small lists but not for the large ones. Thank you

like image 342
Mus Avatar asked Oct 20 '25 02:10

Mus


1 Answers

You can use itertools.product:

import itertools
import pandas as pd

list1 = ["A", "B", "C", "D", "E"]
list2 = ["x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8"]
result = pd.DataFrame(list(itertools.product(list1, list2)))
like image 172
João Victor Avatar answered Oct 21 '25 16:10

João Victor