# 如何通过遍历Python数据框中的每一行将计算的值存储在新列中？

``````  vid2               FStart FEnd cap2                                               VDuration  COS  cap1
0 -_aaMGK6GGw_57_61  0      3    A man grabbed a boy from his collar and threw ...  4          2    A man and woman are yelling at a young boy and...
1 -_aaMGK6GGw_57_61  3      4    A lady is waking up a man lying on a chair and...  4          2    A man and woman are yelling at a young boy and...
2 -_hbPLsZvvo_5_8    0      1    A white dog is barking and a caption is writte...  3          2    a dog barking and cooking with her master in t...
...                ...    ...  ...                                                ...        ...  ...
``````

``````#The function that calculates the similarity score
def get_cosine_similarity(feature_vec_1, feature_vec_2):
return cosine_similarity(feature_vec_1.reshape(1, -1), feature_vec_2.reshape(1, -1))[0][0]

for i, row in merged.iterrows():
captions = []
captions.append(row['cap1'])
captions.append(row['cap2'])

for c in range(len(captions)):
captions[c] = pre_process(captions[c])
captions[c] = lemmatize_sentence(captions[c])

feature_vectors = tfidf_vectorizer.transform(captions)

fsims = get_cosine_similarity(feature_vectors[0], feature_vectors[1])
merged['fsim'] = fsim
``````

``````       fsim
0  0.054464
1  0.054464
2  0.054464
3  0.054464
4  0.054464
``````