Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fastest way to create pandas dataframe rows for combination of values from lists

let's say i have three list

listA = ['a','b','c', 'd']
listP = ['p', 'q', 'r']
listX = ['x', 'z']

so the dataframe will have 4*3*2 = 24 rows. now, the simplest way to solve this problem is to do this:

df = pd.DataFrame(columns=['A','P','X'])

for val1 in listA:
   for val2 in listP:
      for val3 in listX:
         df.loc[<indexvalue>] = [val1,val2,val3]

now in the real scenario I will have about 800k rows and 12 columns (so 12 nesting in the loops). is there any way i can create this dataframe much faster?

like image 959
sjishan Avatar asked Dec 11 '25 14:12

sjishan


1 Answers

Similar discussion here. Apparently np.meshgrid is more efficient for large data (as an alternative to itertools.product.

Application:

v = np.stack(i.ravel() for i in np.meshgrid(listA, listP, listX)).T
df = pd.DataFrame(v, columns=['A', 'P', 'X'])
>>  A  P  X
0   a  p  x
1   a  p  z
2   b  p  x
3   b  p  z
4   c  p  x
like image 128
Tarifazo Avatar answered Dec 13 '25 03:12

Tarifazo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!