How to unnest (explode) a column in a pandas DataFrame?
我相信这个问题与上面列出的问题不同。我试图找到一列中单元格的组合,并由此创建两列。上面的说明显示了如何取消嵌套列表,但不查找该列表的组合…
我有一个数据框,其中一列包含一个列表。我试图扩展这个数据框,这样我就可以得到列表的每一个组合,并且仍然保留其他信息。很难解释,下面是示例数据帧:
name number ID code
1111 2 3 ['%AB','$12','&FD']
我试图找出如何将此数据帧转换为以下内容:
name number ID to from
1111 2 3 %AB $12
1111 2 3 %AB &FD
1111 2 3 $12 &FD
我试过的代码:
a = [y for x in df[['code']].stack() for y in combinations(x,2)]
df[['to','from']] = a
想法是为新数据帧中的索引向元组添加索引(^{} 表示提取列),所以可能是^{} 原始
DataFrame
:相关问题 更多 >
编程相关推荐