去掉句子中特定字符后的部分

2024-07-04 16:41:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我的专栏里有很多句子。在每一个句子中,我都试图去掉一个单词“In”后面的一部分。示例:

当前表格:“玛丽的房子里有很多家具,她和父母以及男友住在马里兰州。”

理想的形式:“玛丽的房子里有很多家具,她和她的父母和男友住在一起。”

我尝试过多种解决方案,但是在每种情况下,在字符串“in”的任何实例之后,句子都会被分开,即使它在一个单词中。所以现在,我的结果是:“玛丽有很多家具”。那是因为里面的单词包含字符串“in”

这是我目前所拥有的,它没有产生预期的输出:

 df['split'] = df.sentences.apply(lambda x: "in".join(x.split("in", 1)[:1]))

任何帮助都将不胜感激


Tags: 字符串in示例df单词句子表格split
2条回答

就快到了,只需在单词in前后加一个额外的空格,就像这样' in '

df['split'] = df.sentences.apply(lambda x: " in ".join(x.split(" in ", 1)[:1]))

输出:

Mary has a lot of furniture inside her house, where she lives with her parents and her boyfriend

如果单词str.split前后有空格,请使用in并将其拆分

df['split'] = df['sentences'].str.split('\sin\s').str[0]

输出

0    Mary has a lot of furniture inside her house, where she lives with her parents and her boyfriend
Name: sentences, dtype: object

或者在注释中使用Zachary建议的单词边界:

df['split'] = df['sentences'].str.split(r'\bin\b').str[0]

相关问题 更多 >

    热门问题