How to generate 2D mesh from two 1D arrays and convert it into a dataframe?

Viewed 302

For example, I have two arrays:

import numpy as np
x = np.array([1,2,3])
y = np.array([10, 11])

How can I generate a pandas dataframe with every combination of x and y, like below?

x      y
1     10
1     11
2     10
2     11
3     10
3     11
5 Answers
import pandas as pd
import numpy as np

x = np.array([1,2,3])
y = np.array([10, 11])


pd.DataFrame({'x':np.repeat(x,y.shape[0]),
              'y':np.tile(y,x.shape[0])})

yields:

   x   y
0  1  10
1  1  11
2  2  10
3  2  11
4  3  10
5  3  11

You could use pd.Multiindex.from_product:

pd.DataFrame(index=pd.MultiIndex.from_product([x, y])).reset_index()

   level_0  level_1
0        1       10
1        1       11
2        2       10
3        2       11
4        3       10
5        3       11

Or for some reason you want to call the method directly:

from pandas.core.reshape.util import cartesian_product

print (pd.DataFrame(cartesian_product([x, y])).T)

   0   1
0  1  10
1  1  11
2  2  10
3  2  11
4  3  10
5  3  11
import numpy as np
x = np.array([1,2,3])
y = np.array([10, 11])

yv, xv = np.meshgrid(y, x)
df = pd.DataFrame(dict(x=xv.ravel(), y=yv.ravel()))

output:

   x   y
0  1  10
1  1  11
2  2  10
3  2  11
4  3  10
5  3  11

Take the cartesian product of both arrays via itertools.product and build a dataframe from the result:

from itertools import product

pd.DataFrame(product(x, y), columns=['x','y'])

   x   y
0  1  10
1  1  11
2  2  10
3  2  11
4  3  10
5  3  11

A comparison of proposed solutions using benchit:

#@Henry's solution
def m1(x, y):
  pd.DataFrame(index=pd.MultiIndex.from_product([x, y])).reset_index()

#@anon01's solution
def m2(x,y):
  yv, xv = np.meshgrid(y, x)
  return pd.DataFrame(dict(x=xv.ravel(), y=yv.ravel()))

#@My Work's solution
def m3(x,y):
  return pd.DataFrame({'x':np.repeat(x,y.shape[0]),'y':np.tile(y,x.shape[0])})

#@yatu's solution
def m4(x,y):
  return pd.DataFrame(product(x, y), columns=['x','y'])


#@?'s solution (removed)
def m5(x,y):
  return pd.DataFrame([[j, i] for j in x for i in y], columns=['x','y'])

in_ = {n:[np.arange(n), np.arange(n)] for n in [10,100,1000,5000]}

Seems m2 and m3 are fastest for large arrays (m2 slightly faster).

enter image description here

Related