Python 如何展开行包含值列表的数据帧?
我希望输出如下所示:Python 如何展开行包含值列表的数据帧?,python,pandas,Python,Pandas,我希望输出如下所示: c1 c2 c3 0 [1, 2] [[a, b], [c, d, e]] [[aff , bgg], [cff, ddd, edd]] 您可以使用np.repeat()和chain.from\u iterable(): 返回: df = pd.DataFrame({'c1': np.repeat(df['c1'].values[0], [len(x) fo
c1 c2 c3
0 [1, 2] [[a, b], [c, d, e]] [[aff , bgg], [cff, ddd, edd]]
您可以使用
np.repeat()
和chain.from\u iterable()
:
返回:
df = pd.DataFrame({'c1': np.repeat(df['c1'].values[0], [len(x) for x in (chain.from_iterable(df['c2']))]),
'c2': list(chain.from_iterable(chain.from_iterable(df['c2']))),
'c3': list(chain.from_iterable(chain.from_iterable(df['c3'])))
})
请记住,这是相对特定于您的用例的。它假定您的
c2
和c3
列被实例化为相同的形状。请记住添加到代码中的内容、您尝试使其工作的内容、输出的内容以及希望发生的事情检查dup问题。
df = pd.DataFrame({'c1': np.repeat(df['c1'].values[0], [len(x) for x in (chain.from_iterable(df['c2']))]),
'c2': list(chain.from_iterable(chain.from_iterable(df['c2']))),
'c3': list(chain.from_iterable(chain.from_iterable(df['c3'])))
})
c1 c2 c3
0 1 a aff
1 1 b bgg
2 2 c cff
3 2 d ddd
4 2 e edd