我有一个pandas数据帧func assetAtIndex(index: Int) -> PHAsset {
// Return results in reverse order
return fetchResult[fetchResult.count - index - 1] as! PHAsset
}
,如下所示:
q2
我想知道哪位学生参加过哪些课程并在屏幕上显示。
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
gb = q2.groupby(('StudentID'))
result = gb['Subjects'].unique()
c1=pd.DataFrame({'StudentID':result.index, 'Subjects':result.values})
看起来像这样
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]
我该怎么办?
答案 0 :(得分:2)
我认为你可以apply
运作join
。另外,对于创建DataFrame
,您可以使用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
最后,您可以将StudentID
列投放到str
(如果dtype
为int
)并且可以将其合并在一起:
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
如果原始数据可以覆盖,请使用:
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