bazinga
bazinga

Reputation: 2250

Creating new DataFrame from the cartesian product of 2 lists

What I want to achieve is the following in Pandas:

a = [1,2,3,4]
b = ['a', 'b']

Can I create a DataFrame like:

column1 column2
'a'        1
'a'        2
'a'        3
'a'        4
'b'        1
'b'        2
'b'        3
'b'        4

Upvotes: 1

Views: 264

Answers (2)

jezrael
jezrael

Reputation: 863801

Use itertools.product with DataFrame constructor:

a = [1, 2, 3, 4]
b = ['a', 'b']

from itertools import product

# pandas 0.24.0+
df = pd.DataFrame(product(b, a), columns=['column1', 'column2'])
# pandas below 
# df = pd.DataFrame(list(product(b, a)), columns=['column1', 'column2'])
print (df)
  column1  column2
0       a        1
1       a        2
2       a        3
3       a        4
4       b        1
5       b        2
6       b        3
7       b        4

Upvotes: 8

MEdwin
MEdwin

Reputation: 2960

I will put here another method, just in case someone prefers it.

full mockup below:

import pandas as pd 
a = [1,2,3,4] 
b = ['a', 'b']
df=pd.DataFrame([(y, x) for x in a for y in b], columns=['column1','column2'])
df

result below:

    column1 column2
0   a   1
1   b   1
2   a   2
3   b   2
4   a   3
5   b   3
6   a   4
7   b   4

Upvotes: 0

Related Questions