本质上,你是
重复
或
链式
根据您的列的值。
所以你可以用
np.repeat
和
itertools.chain
视情况而定。该解决方案对于少数列是有效的,如您的示例所示。
import numpy as np
from itertools import chain
# set up dataframe
df = pd.DataFrame({'from': ['abc', 'def', 'gfhi'],
'to': ['xyz', 'uvw', 'rst'],
'obj': [['foo', 'bar'], ['gee'], ['foo', 'bar', 'baz']]})
# calculate length of each list in obj
lens = df['obj'].map(len)
# calculate result, repeating or chaining as appropriate
res = pd.DataFrame({'from': np.repeat(df['from'], lens),
'to': np.repeat(df['to'], lens),
'obj': list(chain.from_iterable(df['obj']))})
print(res)
from to obj
0 abc xyz foo
0 abc xyz bar
1 def uvw gee
2 gfhi rst foo
2 gfhi rst bar
2 gfhi rst baz