2017-04-10 72 views
2

在python中我想根據第一個數字的值將不同行中的文本分開。所以:根據第一列中的值排序第二列中的文本

Harry went to School 100 
Mary sold goods 50 
Sick man 

使用下面提供的信息:

number text 
1 Harry 
1 Went 
1 to 
1 School 
1 100 
2 Mary 
2 sold 
2 goods 
2 50 
3 Sick 
3 Man 

for i in xrange(0, len(df['number'])-1): 
    if df['number'][i+1] == df['number'][i]: 
      # append text (e.g Harry went to school 100) 
    else: 
      # new row (Mary sold goods 50) 
+1

它看起來像你全光照g pandas - 是對的嗎? – oLas

+0

是的,這是正確的 – Probs

回答

3

您可以使用groupby

for name,group in df.groupby(df['number']): 
    print ' '.join([i for i in group['text']]) 

結果

Harry Went to School 100 
Mary sold goods 50 
Sick Man 
相關問題