python - 如何将分组的文本一一合并
问题描述
我有一个如下所示的数据框,
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 组失败。
而且我的脚本显然无效。
有人可以提出一种解决我的索引问题和性能问题的方法吗?
任何帮助都是不言而喻的。
解决方案
cumsum
使用s使用函数并不容易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)
推荐阅读
- javascript - 递归 async/await 使 React App 崩溃
- tensorflow - Tensorflow 线性回归房价
- javascript - Javascript 和 Python - Django
- javascript - 为我自己的语言开发 JavaScript linting 工具的最佳实践?
- php - 尝试打印命令的实时输出时,在 system() 命令之后未执行 PHP 代码
- javascript - 获取国家/地区的 id 会返回 Expression 'mid = s.id' is non-assignable
- mysql - SQL命令二合并
- r - R:将等级 1 分配给预定义的最大值
- c# - 如何将以下字符串再次转换为日期字符串?
- php - 减少 MD5 - 使用不同的基数