<p>如果列中有列表,请将<a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.join.html" rel="nofollow noreferrer">^{<cd1>}</a>与<a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.get_dummies.html" rel="nofollow noreferrer">^{<cd2>}</a>一起使用:</p>
<pre><code>df = df['my column'].str.join('|').str.get_dummies()
print (df)
A B C D
0 1 1 0 0
1 0 1 1 1
2 0 1 0 1
</code></pre>
<p>或<a href="http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MultiLabelBinarizer.html" rel="nofollow noreferrer">^{<cd3>}</a>:</p>
<pre><code>from sklearn.preprocessing import MultiLabelBinarizer
mlb = MultiLabelBinarizer()
df = pd.DataFrame(mlb.fit_transform(df['my column']),columns=mlb.classes_)
print (df)
A B C D
0 1 1 0 0
1 0 1 1 1
2 0 1 0 1
</code></pre>
<p>如果有字符串,请将<a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.strip.html" rel="nofollow noreferrer">^{<cd4>}</a>与<code>str.get_dummies</code>一起使用,如果需要,请从列名中删除<code>"</code>:</p>
<pre><code>df = (df['my column'].str.strip('[]')
.str.get_dummies(', ')
.rename(columns=lambda x: x.strip('"')))
print (df)
A B C D
0 1 1 0 0
1 0 1 1 1
2 0 1 0 1
</code></pre>