我想比较句子的两个较长的Dataframe列,并返回看起来像这样的第三个数据框。 快照如下所示。
我的第一种方法长期存在,仅适用于单个实例,但是当我将其应用于数据框时失败了。可以在上一个问题中找到它。
逻辑适用于c1和c2中的单词,新值= 1,仅适用于c1中的单词,值设为零。
sentences = tra_df['Sent1']
context = tra_df['Sent2']
Sent1[0] = "I am completely happy with the plan you have laid out today"
Sent2[0] = 'the plan you have laid out today'
c3 = ['0', '0', '0', '0' , '0', '1', '1', '1', '1', '1', '1']
答案 0 :(得分:1)
根据我对您问题的理解,这是解决方案。
def get_common_words(c1, c2):
res = [0]*len(c1.split())
for idx, existing_word in enumerate(c1.split()):
if existing_word in c2.split():
res[idx] = 1
return res
get_common_words(c1, c2)
如果要使其适用于熊猫数据框
def get_common_words_df(row):
c1 = row['Sent1']
c2 = row['Sent2']
return get_common_words(c1, c2)
df['sent3'] = df.apply(get_common_words_df, axis=1)
您可以对其进行很多优化