Group unique 0th elements of CSV for unique ith elements in python or hive
<p>Please see the image at link to best see the input and required output formats and read description below</p>

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

<p>I'm seeking to take a 3 (or 2) column csv and create a new csv where for each unique 1st element (ie 2nd column) all the unique 0th elements are grouped so the that the structure of output csv rows are as such:
unique 1st element, unique 0th element #1, unique 0th element #2,...</p>

<p>Using Python 3.x or Python 2.x or Hive or SQL. Very much appreciate any suggestions. Thank you!</p>

<p>You can do it this way:</p>

<pre><code>In [34]: df
Out[34]:
    c1  c2
0    1  p1
1    1  p1
2    1  p2
3    2  p2
4    2  p3
5    3  p3
6    3  p3
7    3  p3
8    3  p4
9    3  p4
10   3  p5

In [36]: (df.groupby('c2')['c1']
   ....:    .apply(lambda x: ','.join(x.unique().astype(str)))
   ....:    .to_frame('unique').to_csv(r'D:/temp/output.csv')
   ....: )
</code></pre>

<p>output.csv:</p>

<pre><code>c2,unique
p1,1
p2,"1,2"
p3,"2,3"
p4,3
p5,3
</code></pre>

python csv pandas hive itertools