2017-04-24 59 views

回答

3
df = pd.DataFrame({0: [['hello', 'motto'], ['motto', 'mania']]}) 
print(df) 

       0 
0 [hello, motto] 
1 [motto, mania] 

使用str.join其次str.get_dummies

df[0].str.join('|').str.get_dummies() 

    hello mania motto 
0  1  0  1 
1  0  1  1 
+0

謝謝!當我被允許時會接受答案! – laila

+0

@laila歡迎您。 – piRSquared

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