df = pd.DataFrame({'A': [[1, 2, 3], 'foo', [], [3, 4]], 'B': 1})
>>> df
A B
0 [1, 2, 3] 1
1 foo 1
2 [] 1
3 [3, 4] 1
如上,A列为数组类型,其实在pandas中就是用中括号括起来,元素用逗号分隔开的字符串;接下来对A列分成多行,对B列产生笛卡尔积;
df.explode('A')
A B
0 1 1
0 2 1
0 3 1
1 foo 1
2 NaN 1
3 3 1
3 4 1