python - 将一列的单词与其他列python中的所有其他单词连接起来
问题描述
这是数据集
pd.DataFrame({'Word':['Iron','copper','nickel'],'Sentence':['An Apple a day','Roses are red','Skies are blue']})
我正在尝试将一个单词与其他单词连接起来,并将其附加到一个新列中,这样我的输出看起来就像
Word Sentence New_Sentence
0 Iron An Apple a day An_Iron Apple_Iron a_Iron day_Iron
1 copper Roses are red Roses_copper are_copper red_copper
2 nickel Skies are blue skies_nickel are_nickel blue_nickel
有什么建议么?
解决方案
尝试这个,
>>> df['New_Sentence'] = df.apply(lambda x: ' '.join(['_'.join(i) for i in [(s, x['Word'])
for s in x['Sentence'].split()]]), axis=1)
>>> df
Word Sentence New_Sentence
0 Iron An Apple a day An_Iron Apple_Iron a_Iron day_Iron
1 copper Roses are red Roses_copper are_copper red_copper
2 nickel Skies are blue Skies_nickel are_nickel blue_nickel
推荐阅读
- c# - .NET Core 避免使用 StaticFileMiddleware 返回响应 304 未修改
- elasticsearch - lucene 邻近搜索如何处理多个乱序词?
- java - 序列化对象中包含的每个对象是否也必须序列化?(安卓、Java)
- python - ValueError:tile 无法扩展到图像 pytorch 之外
- django - 如何显示与特定用户相关的信息
- javascript - 显示按钮单击并将该数据存储在本地存储中的计数器
- trigonometry - wxMaxima 变换三角表达式
- docker - 在 Dockerfile.template 中运行 Django 迁移似乎会跳过并直接运行服务器
- html - 你能在 Angular 的现有组件中添加一个模态对话框吗?
- android - 从安装包中的 notificationcompact 获取信息