我有一个有四个nw_data='Qn_id','Qn_context','Qns','Anwsers'的结构。
Qn_id | Qn_context | Qns | Anwsers
01 | In 1962, Uk gave... | what year....| the year 1962 was.....
02 | Major kanuti raised..| Who raised...| Kanuti akorimo rasied.
字符串
我想向该数据集添加第五列,该数据集由列“Answers”]的句子嵌入组成。
我正在使用sentence_transformers来生成句子嵌入。
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('all-MiniLM-L6-v2')
型
我尝试使用一种方法,其中:
#Created a var for the column
sent = nw_data['Answers']
型
和
#Passed the variable sent into the model and created the embeddings
embeddings = model.encode(sent)
型
然后
#Tried passing the embeddings into a new column named Embeddings
nw_data['Embeddings'] = embeddings
型
我得到一个错误:
KeyError: 'Embeddings'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
KeyError: 'Embeddings'
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/internals/blocks.py in check_ndim(values, placement, ndim)
1978 if len(placement) != len(values):
1979 raise ValueError(
-> 1980 f"Wrong number of items passed {len(values)}, "
1981 f"placement implies {len(placement)}"
1982 )
ValueError: Wrong number of items passed 384, placement implies 1
型
如何创建这些嵌入并将其添加到同一个嵌套nw_data!!中的新列中?
无论如何,这是可能的,建议尝试使用**.apply()方法或lambda函数**,但问题是我不确定如何或何时使用它们。
2条答案
按热度按时间dfuffjeb1#
如果我理解正确的话,你想在单元格中插入一个列表(嵌入)。
尝试使用
at
:字符串
如果你有多个句子,只需传递列表:
型
inn6fuwd2#
我找到了另一种方法来做到这一点,请告诉我,如果它的工作:
字符串