2014-03-04 28 views
0

來自以下數據框;熊貓:結合行中的文本

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 

回答

1

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.'] 
+0

爽!非常感謝! – richie

+0

@richie np,很樂意幫忙;) – zhangxaochen