如何一个一组地组合文本

时间:2019-02-27 07:45:48

标签: python pandas

我有一个如下数据框,

        text  group
0      hello      1
1      world      1
2       it's      2
3       time      2
4         to      2
5    explore      2
6        one      3
7       more      3
8       line      3

我想像下面一样在新列中逐一组合文本中的每个单词,

        text  group                     result
0      hello      1                      hello
1      world      1                hello world
2       it's      2                       it's
3       time      2                  it's time
4         to      2               it's time to
5    explore      2       it's time to explore
6        one      3                        one
7       more      3                   one more
8       line      3              one more line

到目前为止,我尝试过

df['res']=df.groupby('group')['text'].transform(lambda x: ' '.join(x))
df['result']=df[['text','res']].apply(lambda x: ' '.join( x['res'].split()[:x['res'].split().index(x['text'])+1]),axis=1)

以上代码可解决上述问题。但是它有一些问题。

如果我有重复的文本索引将给我第一个元素位置,则此数据将失败

        text  group                     result
0      hello      1                      hello
1      world      1                hello world
2       it's      2                       it's
3       time      2                  it's time
4         to      2               it's time to
5    explore      2       it's time to explore
6        one      3                        one
7       more      3                   one more
8       line      3              one more line
9      hello      4                      hello
10  repeated      4             hello repeated
11     hello      4                      hello #this must be hello repeated hello
12      came      4  hello repeated hello came

注意:它在第4组失败。

此外,我的脚本显然无效。

有人可以提出一种解决我的索引问题和性能问题的方法吗?

任何帮助都是有意义的。

2 个答案:

答案 0 :(得分:2)

使用cumsum的函数string并不容易,但这是一个可能的解决方案-首先在末尾添加空间,使用cumsum,最后通过从右侧删除空间rstrip

df['text'] = df['text'] + ' '
df['res'] = df.groupby('group')['text'].transform(pd.Series.cumsum).str.rstrip()

替代:

df['res'] = df['text'].add(' ').groupby(df['group']).transform(pd.Series.cumsum).str.rstrip()

print (df)
       text  group                   res
0    hello       1                 hello
1    world       1           hello world
2     it's       2                  it's
3     time       2             it's time
4       to       2          it's time to
5  explore       2  it's time to explore
6      one       3                   one
7     more       3              one more
8     line       3         one more line

另一种解决方案:

f = lambda x: [' '.join(x[:i]) for i in range(1, len(x)+1)]
df['res'] = df.groupby('group')['text'].transform(f)

答案 1 :(得分:0)

在列表理解中使用groupby

df['res'] = [' '.join(d.text[:i]) for _, d in df.groupby('group') for i in range(1, len(d)+1)]

print(df)
        text  group                        res
0      hello      1                      hello
1      world      1                hello world
2       it's      2                       it's
3       time      2                  it's time
4         to      2               it's time to
5    explore      2       it's time to explore
6        one      3                        one
7       more      3                   one more
8       line      3              one more line
9      hello      4                      hello
10  repeated      4             hello repeated
11     hello      4       hello repeated hello
12      came      4  hello repeated hello came