简体   繁体   English

Python函数为每一行返回相同的值

[英]Python function return the same value for each row

i want to pos-tagging a dataset of .csv file.我想对 .csv 文件的数据集进行后标记。 I create a function which i hope can return the value of the pos-tagged words.我创建了一个函数,我希望它可以返回 post-tagged 单词的值。 But the value that return , instead give a value of the row , it return all of the value on every row.但是 return 的值,而不是给出 row 的值,它返回每一行的所有值。 What do i miss?我想念什么? Is it because of the datatype or sum?是因为数据类型还是总和? anyway , here's the code that i wrote不管怎样,这是我写的代码

def postagging(text):
pos_emotion = ct.tag_sents([df.emotion])
return pos_emotion

df['pos_emotion'] = df['emotion'].apply(postagging)
df

and here's the result这是结果

结果

I'm sorry if i don't explain really clear, my english is not so good.对不起,如果我没有解释清楚,我的英语不太好。

You are getting the same value because every time you are passing [df.emotion] that is entire column of the dataframe instead of text that will be the value you want to process.您将获得相同的值,因为每次您传递[df.emotion]时,它都是数据框的整个列,而不是您要处理的值的text

if you want to do postagging for each row you should modify your function in the following way:如果你想为每一行做postagging,你应该通过以下方式修改你的函数:

def postagging(text):
   pos_emotion = ct.tag_sents([text])
   return pos_emotion

hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM