【发布时间】:2020-10-09 16:29:19
【问题描述】:
我有一个数据框和一个列表如下:
data = {{"text":["I have one apple and two bananas", "this is my apple", "she has three apples","My friend has five apples but she only has one banana"]}
df= pd.DataFrame(data=data, columns=['text'])
my_list = ['one','two','three','four','five']
我想要输出的是一个额外的列“new_text”,其中包含列表中单词的句子被替换为 my_list 中的每个单词,因此输出如下所示:
output:
text new_text
0 I have one apple and two bananas I have two apple and three bananas, I have three apple and four bananas,I have four apple and five bananas,I have five apple and one bananas,...
1 this is my apple this is my apple
2 she has three apples she has two apples,she has four apples,she has five apples,...
and so on...
同句和复数的重复无关紧要,唯一重要的是列表中的所有单词都出现在'new_text'列的句子中
第 1 步有一个例外,但它只找到第一个单词:
data1 = data['text'].str.extract(
r"(?i)(?P<before>.*)\s(?P<clock>\(?=\bone\b | \btwo\b | \bthree\b | \bfour\b | \bfive\b))\s(?P<after>.*)")
提前谢谢你
【问题讨论】: