팬더

2016-10-26 20 views
2

에서 텍스트의 행을 결합하는 방법은 두 개의 열이있는 테이블을 가지고 있고 나는이 같은 결과가 필요팬더

import pandas as pd 
df = DataFrame({'id':[101453,101465,101478,101453,101465,101465], 'text' :['this','is','a','test','string','one']}) 

같은 ID로 텍스트를 결합하려는 :

df = DataFrame({'id':[101453,101465,101478], 'text':['this test','is string one','a']}) 

답변

2

applyjoingroupby을 사용

print (df.groupby('id')['text'].apply(' '.join).reset_index()) 
     id   text 
0 101453  this test 
1 101465 is string one 
2 101478    a 
1
df['id'] = sorted(list(set(df['id']))) 

set()은 모든 동일한 요소를 제거합니다. 그런 다음 list()로 리턴하십시오. 필요한 경우 정렬하십시오.