Dataframe并传递一个变量以填充列

2024-06-01 22:36:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python新手。我有这样一个数据帧:

      name             comment        
0      A            I enjoy this lovely moment.
1      B            I can't believe this was the worst day ever.
2      C            The hotel was amazing.
3      A            I am overwhelmed by this lovely journey.
4      C            Food is good as well as having a nice time.
...

我必须在该数据框中创建一个新列,并在该列中为每行填充一个名为“a”的变量

code for my dataset is:

a=pd.read\U csv(io.StringIO(r.decode('utf-8'))

Can you please help me please please.

Tags: the数据nameisascommentthiscan
3条回答

要转换概率,请使用

label_probs, attentions = model_with_attentions.predict(encoded_samples)
label_probs = {id2label[_id]: prob for (label, _id), prob in zip(label2id.items(),label_probs[0])}

在这里,你可以预测情绪

emotions = [label for label, _ in label_probs.items()]

然后插入这个情绪变量,正如我在上面的第一条评论中解释的

好吧,我假设您有一个名为rawData的数据框架,它有两列['name', comment']。 要在rawData中添加新列,可以添加一行:

rawData['prediction'] = Prediction

您将得到具有三列的rawData,即['name', comment', 'prediction']

您可以创建一个函数predict,该函数接收一条注释并返回预测值,然后使用以下代码创建一个新列:

rawData['prediction'] = rawData['comment'].apply(lambda x: predict(x))

要在python中创建函数,语法如下:

def predict(comment):
    # code that does the prediction
    return prediction

相关问题 更多 >