简体   繁体   中英

Display Pandas DataFrame in csv format

I have a pandas dataframe q2 which looks like this:

    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

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

gb = q2.groupby(('StudentID'))
result = gb['Subjects'].unique()

c1=pd.DataFrame({'StudentID':result.index, 'Subjects':result.values})

c1 looks like this

   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]

However, the desired output is the following:

323: History, Physics
999: Chemistry, History, Physics
1234: Chemistry
2834: Physics
3455: Chemistry, History, Mathematics
56767: Mathematics

what can I do?

I think you can apply function join . Also for creating DataFrame you can use reset_index :

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

Last you can cast column StudentID to str (if dtype is int ) and concanecate together:

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  

Also if original data can be overwrite, use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM