Display Pandas DataFrame in csv format
<p>I have a pandas dataframe <code>q2</code> which looks like this:</p>

<pre><code>    StudentID     Subjects
6         323      History
9         323      Physics
8         999    Chemistry
7         999      History
4         999      Physics
0        1234    Chemistry
5        2834      Physics
1        3455    Chemistry
2        3455      History
10       3455  Mathematics
3       56767  Mathematics
</code></pre>

<p>I want to find out which student has taken which courses and display it on screen.</p>

<pre><code>gb = q2.groupby(('StudentID'))
result = gb['Subjects'].unique()

c1=pd.DataFrame({'StudentID':result.index, 'Subjects':result.values})
</code></pre>

<p><code>c1</code> looks like this</p>

<pre><code>   StudentID                           Subjects
0        323                 [History, Physics]
1        999      [Chemistry, History, Physics]
2       1234                        [Chemistry]
3       2834                          [Physics]
4       3455  [Chemistry, History, Mathematics]
5      56767                      [Mathematics]
</code></pre>

<p>However, the desired output is the following:</p>

<pre><code>323: History, Physics
999: Chemistry, History, Physics
1234: Chemistry
2834: Physics
3455: Chemistry, History, Mathematics
56767: Mathematics
</code></pre>

<p>what can I do?</p>

<p>I think you can <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.apply.html" rel="nofollow"><code>apply</code></a> function <code>join</code>. Also for creating <code>DataFrame</code> you can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.reset_index.html" rel="nofollow"><code>reset_index</code></a>:</p>

<pre><code>gb = q2.groupby(('StudentID'))
result = gb['Subjects'].unique()

c1 = result.reset_index()

c1.Subjects = c1.Subjects.apply(', '.join)
print (c1)
   StudentID                         Subjects
0        323                 History, Physics
1        999      Chemistry, History, Physics
2       1234                        Chemistry
3       2834                          Physics
4       3455  Chemistry, History, Mathematics
5      56767                      Mathematics
</code></pre>

<p>Last you can cast column <code>StudentID</code> to <code>str</code> (if <code>dtype</code> is <code>int</code>) and concanecate together:</p>

<pre><code>c1['new'] = c1.StudentID.astype(str) + ':' + c1.Subjects
print (c1)
   StudentID                         Subjects  \
0        323                 History, Physics   
1        999      Chemistry, History, Physics   
2       1234                        Chemistry   
3       2834                          Physics   
4       3455  Chemistry, History, Mathematics   
5      56767                      Mathematics   

                                    new  
0                  323:History, Physics  
1       999:Chemistry, History, Physics  
2                        1234:Chemistry  
3                          2834:Physics  
4  3455:Chemistry, History, Mathematics  
5                     56767:Mathematics  
</code></pre>

<p>Also if original data can be overwrite, use:</p>

<pre><code>result = result.index.to_series().astype(str) + ':' + result.apply(', '.join)
print (result)
StudentID
323                      323:History, Physics
999           999:Chemistry, History, Physics
1234                           1234:Chemistry
2834                             2834:Physics
3455     3455:Chemistry, History, Mathematics
56767                       56767:Mathematics
dtype: object
</code></pre>

python list pandas dataframe unique