python pandas groupby sorting and concatenating
<p>I have a panda data frame:</p>

<pre><code>df = pd.DataFrame({'a': [1,1,1,1,2,2,2], 'b': ['a','a','a','a','b','b','b'], 'c': ['o','o','o','o','p','p','p'], 'd': [ [2,3,4], [1,3,3,4], [3,3,1,2], [4,1,2], [8,2,1], [0,9,1,2,3], [4,3,1] ], 'e': [13,12,5,10,3,2,5] })
</code></pre>

<p>What I want is:</p>

<p>First group by columns a, b, c --- there are two groups</p>

<p>Then sort within each group according to column e in an ascending order</p>

<p>Lastly concatenate within each group column d</p>

<p>So the result I want is:</p>

<pre><code>result = pd.DataFrame({'a':[1,2], 'b':['a','b'], 'c':['o','p'], 'd':[[3,3,1,2,4,1,2,1,3,3,4,2,3,4],[0,9,1,2,3,8,2,1,4,3,1]]})
</code></pre>

<p>Could anyone share some quick/elegant ways to get around this? Thanks very much.</p>

<p>You can sort by column <code>e</code>, group by <code>a</code>, <code>b</code> and <code>c</code> and then use a list comprehension to concatenate the <code>d</code> column (flatten it). Notice that we can use <code>sort</code> and then <code>groupby</code> since groupby will </p>

<blockquote>
  <p>preserve the order in which observations are sorted within each group:</p>
</blockquote>

<p>according to the <a href="http://pandas.pydata.org/pandas-docs/stable/groupby.html" rel="nofollow">doc here:</a></p>

<pre><code>(df.sort_values('e').groupby(['a', 'b', 'c'])['d']
                    .apply(lambda g: [j for i in g for j in i]).reset_index())
</code></pre>

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

<p>An alternative to list-comprehension is the chain from itertools:</p>

<pre><code>from itertools import chain
(df.sort_values('e').groupby(['a', 'b', 'c'])['d']
                    .apply(lambda g: list(chain.from_iterable(g))).reset_index())
</code></pre>

python pandas dataframe group-by