How to separate a numpy array into separate columns in pandas
<p>I have a dataframe that looks like</p>

<pre><code>  ID_0 ID_1  ID_2
0    a    b  0.05
1    a    b  0.10
2    a    b  0.19
3    a    c  0.25
4    a    c  0.40
5    a    c  0.65
6    a    c  0.71
7    d    c  0.95
8    d    c  1.00
</code></pre>

<p>I want to groupby and make a normalized histogram of the ID_2 column for each group. So I do</p>

<pre><code>df.groupby(['ID_0', 'ID_1']).apply(lambda x: np.histogram(x['ID_2'], range = (0,1), density=True)[0]).reset_index(name='ID_2')
</code></pre>

<p>However what I would really like is for the 11 elements of the numpy arrays to be in separate columns  of the dataframe.  </p>

<p>How can I do this?</p>

<p>You can construct a series object from each numpy array and the elements will be broadcasted as columns:</p>

<pre><code>import pandas as pd
import numpy as np
df.groupby(['ID_0', 'ID_1']).apply(lambda x: pd.Series(np.histogram(x['ID_2'], range = (0,1), density=True)[0])).reset_index()
</code></pre>

<p><a href="http://i.stack.imgur.com/fc63F.png" rel="nofollow"><img src="http://i.stack.imgur.com/fc63F.png" alt="enter image description here"></a></p>

python pandas numpy