显示大熊猫据帧csv格式
问题描述:
我有一个熊猫数据帧q2
看起来像这样:显示大熊猫据帧csv格式
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]
然而,理想的输出如下:
323: History, Physics
999: Chemistry, History, Physics
1234: Chemistry
2834: Physics
3455: Chemistry, History, Mathematics
56767: Mathematics
我能做些什么?
答
我想你可以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
)和concanecate在一起:
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
谢谢,但它仍然不像预期的输出,请看上面! – GKS
@GKS - 谢谢,对不起,我错过了。现在是对的? – jezrael
完美!谢谢 :-) – GKS