假设我有一个如下所示的DataFrame:
df = pd.DataFrame({'baz': ['yes','fine'], 'foo': [['a', 'b', 'c'], ['s', 'r']]},
index=['W1', 'W2'])
> df
baz foo
W1 yes [a, b, c]
W2 fine [s, r]
如何展开专栏foo
?在这种情况下,结果应该是:
> df
baz foo
W1 yes a
yes b
yes c
W2 fine s
fine r
答案 0 :(得分:2)
您可以通过提取值并重新创建索引来重建DataFrame:
result = []
[result.extend(zip(([df.baz[i]])*len(df.foo[i]), df.foo[i])) for i in range(len(df.baz))]
index = [[df.index[i]]*len(df.foo[i]) for i in range(len(df.baz))]
index = [item for sublist in index for item in sublist] # flatten
final = pd.DataFrame(result,index=index, columns = df.columns) # final result
结果将是:
baz foo
W1 y a
W1 y b
W1 y c
W2 z s
W2 z r