python - 列中的标题词(某些词除外)
问题描述
除了列表中的单词之外,我如何命名所有单词,保留?
keep = ['for', 'any', 'a', 'vs']
df.col
``
0 1. The start for one
1 2. Today's world any
2 3. Today's world vs. yesterday.
预期输出:
number title
0 1 The Start for One
1 2 Today's World any
2 3 Today's World vs. Yesterday.
我试过
df['col'] = df.col.str.title().mask(~clean['col'].isin(keep))
解决方案
这是使用str.replace
和传递替换函数的一种方法:
def replace(match):
word = match.group(1)
if word not in keep:
return word.title()
return word
df['title'] = df['title'].str.replace(r'(\w+)', replace)
number title
0 1 The Start for One
1 2 Today'S World any
2 3 Today'S World vs. Yesterday.
推荐阅读
- database - DynamoDB 是否保证在快速更新时收敛到最新数据?
- python - 阶乘数系统算法的形式证明
- python - Keras 中单一维度的全局运算符?
- vb.net-2010 - Process.Start 从其他表单的文本框中删除文本
- vb.net - 为什么我的类中的公共只读属性不显示???(视觉工作室 2019)
- c# - 为什么是 ImmutableList
一个类但 ImmutableArray 一个结构? - mediawiki - 带有链接和内联 html 标签的 MediaWiki 模板
- python - 使用 Python 将 JSON 内容写入 HDFS 位置
- shell - 无法创建先进先出“[Errno 17] 文件存在”
- variables - 将变量的多个值设置为“选项”