除了列表中的单词,我如何命名所有单词,保留?
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.
关于python - 列中除某些词外的标题词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66348567/