get_dummies split character
<p>I have data labelled which I need to apply one-hot-encoding: <code>'786.2'</code>, <code>'ICD-9-CM|786.2'</code>, <code>'ICD-9-CM'</code>, <code>'786.2b|V13.02'</code>, <code>'V13.02'</code>, <code>'279.12'</code>, <code>'ICD-9-CM|V42.81'</code> is labels. The <code>|</code> mean that the document have 2 labels at the same time. So I wrote the code like this:</p>

<pre><code>labels = np.asarray(label_docs)

labels = np.array([u'786.2', u'ICD-9-CM|786.2', u'|ICD-9-CM', u'786.2b|V13.02', u'V13.02', u'279.12', u'ICD-9-CM|V42.81|'])

df = pd.DataFrame(labels, columns=['label'])
labels = df['label'].str.get_dummies(sep='|')
</code></pre>

<p>and the result: </p>

<pre><code>279.12  786.2  786.2b  ICD-9-CM  V13.02  V42.81
0       0      1       0         0       0       0
1       0      1       0         1       0       0
2       0      0       0         1       0       0
3       0      0       1         0       1       0
4       0      0       0         0       1       0
5       1      0       0         0       0       0
6       0      0       0         1       0       1
</code></pre>

<p>However, now I only want 1 label for each document: </p>

<p><code>'ICD-9-CM|786.2'</code> is <code>'ICD-9-CM'</code>,</p>

<p><code>'ICD-9-CM|V42.81|'</code> is <code>'ICD-9-CM'</code>.</p>

<p>How could I do seperate by <code>get_dummies</code> like that?</p>

<p>I think you need <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.strip.html" rel="nofollow"><code>str.strip</code></a> and <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.split.html" rel="nofollow"><code>str.split</code></a> and then select first item of list by <code>str[0]</code>:</p>

<pre><code>print (df.label.str.strip('|').str.split('|').str[0])
0       786.2
1    ICD-9-CM
2    ICD-9-CM
3      786.2b
4      V13.02
5      279.12
6    ICD-9-CM
Name: label, dtype: object

labels = df.label.str.strip('|').str.split('|').str[0].str.get_dummies()
print (labels)
   279.12  786.2  786.2b  ICD-9-CM  V13.02
0       0      1       0         0       0
1       0      0       0         1       0
2       0      0       0         1       0
3       0      0       1         0       0
4       0      0       0         0       1
5       1      0       0         0       0
6       0      0       0         1       0
</code></pre>

<p>If in row with index <code>2</code> need no value, remove <code>str.strip</code>:</p>

<pre><code>print (df.label.str.split('|').str[0])
0       786.2
1    ICD-9-CM
2            
3      786.2b
4      V13.02
5      279.12
6    ICD-9-CM
Name: label, dtype: object

labels = df.label.str.split('|').str[0].str.get_dummies(sep='|')
print (labels)

   279.12  786.2  786.2b  ICD-9-CM  V13.02
0       0      1       0         0       0
1       0      0       0         1       0
2       0      0       0         0       0
3       0      0       1         0       0
4       0      0       0         0       1
5       1      0       0         0       0
6       0      0       0         1       0
</code></pre>

python pandas one-hot-encoding