pandas - Python apply a func to two lists of lists, store the result in a Dataframe -


to simplify problem, have 2 lists of lists , function shown below:

op = [[1,2,3],[6,2,7,4],[4,1],[8,2,6,3,1],[6,2,3,1,5], [3,1],[3,2,5,4]] ap = [[2,4], [2,3,1]]  def f(lista, listb):     return len(lista+listb) # real f returns number 

i want f(op[i],ap[j]) each i, j, idea create pandas.dataframe looks this:

            ap[0]          ap[1] op[0]   f(ap[0],op[0])  f(ap[1],op[0]) op[1]   f(ap[0],op[1])  f(ap[1],op[1]) op[2]   f(ap[0],op[2])  f(ap[1],op[2]) op[3]   f(ap[0],op[3])  f(ap[1],op[3]) op[4]   f(ap[0],op[4])  f(ap[1],op[4]) op[5]   f(ap[0],op[5])  f(ap[1],op[5]) op[6]   f(ap[0],op[6])  f(ap[1],op[6]) 

my real data has around 80,000 lists in op , 20 lists in ap, , function f little bit time consuming, computational cost should worried.

my idea achieve goal constructing pandas.series of length len(ap)for each op, , append series final dataframe. example, op[0], first create series have information f(op[0],ap[i]) each i.

i stuck constructing series. tried pandas.series.apply() , map()but neither or them worked since function f needs 2 parameters.

i'm open other suggestions f(op[i],ap[j]) each i, j, thanks.

you nested list comprehension, followed application of pandas.dataframe.from_records:

import pandas pd  records = [tuple(f(a, o) in ap) o in op] pd.dataframe.from_records(records) 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -