transform each row to several columns dataframe Pandas DataFrame
<p>I have a Pandas dataframe,whose every row consists of a list of indexes (one column named "Indexes" which has a comma separated string of value):</p>

<p>'Indexes'</p>

<p>'1,4,6,3,2,5,6,8'</p>

<p>'1,7,5,10,23,50'</p>

<p>I want to use apply in order to create a new matrix (DataFrame?), whose every row, has 1s at the values listed in the respective row in the original dataframe, and 0 elsewhere.  Assume that i know the number of new columns, as i have the min and max indexes (say 0 and 10). I can do it using iterrows/itertuples and construct a new matrix, but is there a more efficient way of doing that? I have a matrix of 1Million rows, but even at 100k this quite a long time.</p>

<p>[EXAMPLE]:</p>

<p>i'll an example and desired output for ease of understanding:</p>

<p>assume MAX_INDEX = 4 and MIN_INDEX=0:</p>

<p>for input:</p>

<p>'1,3,2,4'</p>

<p>'0,1'</p>

<p>the output will be 5-column matrix with two rows as follows:</p>

<p>0 1 1 1 1</p>

<p>1 1 0 0 0</p>

<p>[EDIT]
question remains - although i figured out it was very slow just because i created the new matrix using append, instead of allocating the memory a-priori and only inserting new rows. so one solution is:</p>

<pre><code>m=np.empty(shape=[df.shape[0],numFeatures])
i=0
for row in df.itertuples():
    idxs = row[4] # column of peattributes
    idxs = map(lambda(x):int(x),idxs.split(','))
    r=np.zeros(numFeatures)
    r[idxs] = 1
    m[i,]=r
    i+=1
</code></pre>

<p>thanks,
Dan</p>