我有一个 Python 中的事务/元组列表,其中包含不同数量或元素,如下所示:
lst = [('apple','banana','carrots'),('apple',),('banana','carrots',)]
我想以表格形式(最好是 pd.DataFrame
)存储这个列表,例如:
apple banana carrots
0 1 1 1
1 1 0 0
2 0 1 1
但是如果尝试直接使用 pd.DataFrame
进行转换,我会得到他的:
pd.DataFrame(lst)
0 1 2
0 apple banana carrots
1 apple None None
2 banana carrots None
如何将这种类型的列表转换为二进制表?
最佳答案
如果您在列上使用 value_counts
就非常简单,即
pd.DataFrame(lst).apply(pd.value_counts,1).fillna(0)
apple banana carrots
0 1.0 1.0 1.0
1 1.0 0.0 0.0
2 0.0 1.0 1.0
关于python - 元组列表到二进制表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47783833/