首页 > 解决方案 > 列中的标题词(某些词除外)

问题描述

除了列表中的单词之外,我如何命名所有单词,保留?

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))

标签: pythonpython-3.xpandas

解决方案


这是使用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.

推荐阅读