来自以下数据框;
data1 = pd.DataFrame({'Section':[1,1,1,2,2,2,2],'Sub':['What','is','this?','I','am','not','sure.']})
如何获得与此类似的结果;
['What is this?','I am not sure.']
到目前为止,我只能提出像这样的groupby
;
for d in data1.groupby(['Section'])['Sub']:
print d[1]
给你这样的东西;
0 What
1 is
2 this?
Name: Sub, dtype: object
3 I
4 am
5 not
6 sure.
Name: Sub, dtype: object
答案 0 :(得分:2)
join
有空格的项目:
In [34]: for d in data1.groupby(['Section'])['Sub']:
...: print ' '.join(d[1])
What is this?
I am not sure.
并将它们列为一个列表:
In [35]: [' '.join(d[1]) for d in data1.groupby(['Section'])['Sub']]
Out[35]: ['What is this?', 'I am not sure.']