【发布时间】:2025-11-28 18:45:01
【问题描述】:
我有一个数据框,其中一些句子包含单词 'o'clock',我想用我拥有的小时列表替换之前提到的时间,并将新句子放在另一列中,如下所示:
data= {"sentences":["I have a class at ten o'clock", "she is my friend", "she goes to school at eight o'clock"]}
my_list=['two', 'three','five','ten']
我希望看到的是一个额外的列,新句子如下所示,其中时间更改为列表中的所有时间:
输出:
sentences new_sentences
0 I have a class at ten o'clock I have a class at two o'clock, I have a class at three o'clock,...
1 she is my friend she is my friend
2 she goes to school at eight o'clock she goes to school at two o'clock,....
new_sentences 列中的重复是可以的。我曾尝试使用 np.where:
np.where(data.str.contains('o\'clock', regex=False, case=False, na=False), data["sentence"].replace()... )
但我不知道如何替换'o'clock之前的单词
提前谢谢你
【问题讨论】:
标签: python regex pandas list dataframe