将熊猫列表转换为虚拟变量
答
df = pd.DataFrame({0: [['hello', 'motto'], ['motto', 'mania']]})
print(df)
0
0 [hello, motto]
1 [motto, mania]
df[0].str.join('|').str.get_dummies()
hello mania motto
0 1 0 1
1 0 1 1
答
下面是一个存储器中保存的解决方案这是会使用稀疏矩阵和Pandas.SparseSeries:
结果:
In [81]: df
Out[81]:
hello mania motto
0 1 0 1
1 0 1 1
In [82]: df.memory_usage()
Out[82]:
Index 80
hello 8 # notice memory usage: # of ones multiplied by 8 bytes (int64)
mania 8
motto 16
dtype: int64
谢谢!当我被允许时会接受答案! – laila
@laila欢迎您。 – piRSquared