gpt4 book ai didi

python - 列中除某些词外的标题词

转载 作者:行者123 更新时间:2023-12-04 11:13:56 27 4
gpt4 key购买 nike

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

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com