根据第一列中的值对第二列中的文本进行排序

时间:2017-04-10 09:51:36

标签: python pandas

在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 个答案:

答案 0 :(得分: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