Python Textblob tweets:TypeError:传递给`\uuu init\uuuu(text)`的'text'参数必须是字符串,而不是<;类别';熊猫.core.series.series'>,行是列表

Python Textblob tweets:TypeError:传递给`\uuu init\uuuu(text)`的'text'参数必须是字符串,而不是<;类别';熊猫.core.series.series'>,行是列表,python,pandas,dataframe,nlp,textblob,Python,Pandas,Dataframe,Nlp,Textblob,定义从推文中获取情感的功能:(由我撰写) 调用函数获取情感: from textblob import TextBlob get_tweet_sentiment(df['tweet_content']) TypeError: The `text` argument passed to `__init__(text)` must be a string, not <class 'pandas.core.series.Series'> 错误: from textblob impor

定义从推文中获取情感的功能:(由我撰写)

调用函数获取情感:

from textblob import TextBlob 
get_tweet_sentiment(df['tweet_content'])
TypeError: The `text` argument passed to `__init__(text)` must be a string, not <class 'pandas.core.series.Series'>
错误:

from textblob import TextBlob 
get_tweet_sentiment(df['tweet_content'])
TypeError: The `text` argument passed to `__init__(text)` must be a string, not <class 'pandas.core.series.Series'>

请帮助如何更改函数或调用函数的方式。因此,您的问题是函数
获取tweet\u情绪()
需要
字符串作为输入,而您试图将
pd.Series()
作为输入传递。因此,解决方案是使用
df.apply()
lambda
,它只需在每一行运行
get\u tweet\u touction()
。但同样地,
df['tweet\u content']
列中的每个单元格都是一个列表,其中每个单词都作为一个单独的
string
元素包含。要使事情正常运行,您可以使用
'.join(x)
-它从列表
['Hi','I','really','like','you']
字符串
Hi I really like you
-创建,您可以将其传递给函数

代码:

输出:

   tweet_content
0     [Hi, Bird]
1  [Hello, Bear]
Hi Bird
Hello Bear

也许你们得到了列表作为输入,所以从列表中生成字符串会有帮助吗?类似于:
get\u tweet\u情绪('''.join(df['tweet\u content'].values.tolist())
Hi,我在标记化后没有得到列表……所以即使我的列表保持原样,也不能这样做……实际上,列表结构是制作word cloudTry
df['tweet content']所需要的。apply(lambda x:get\u tweet\u情绪(x),axis=1)
。这将把函数应用到该列的每一行。@formicaman,函数所需的字符串,每个单元格都是列表,因此建议稍微调整一下:
df['tweet content']。apply(lambda x:get\u tweet\u情绪(''.join(x)),axis=1)
如果axis=1,@OYisiyl您的代码正在工作
   tweet_content
0     [Hi, Bird]
1  [Hello, Bear]
Hi Bird
Hello Bear