【问题标题】:how to recall def function using juypter notebook如何使用 jupyter notebook 调用 def 函数
【发布时间】:2021-03-08 20:16:24
【问题描述】:

我想使用 juypter notebook 应用情绪分析, 这是我的代码

pos_list = positive_words['Positive'].values.tolist()

def pos_sentiment(推文): 计数=0 对于 pos_list 中的单词: 如果推文中的单词: 计数+=1 返回计数

clean_data['pos_count'] = clean_data['tweetText'].apply(lambda x: pos_sentiment(x))

出现此错误


TypeError Traceback(最近一次调用最后一次) 在 ----> 1 clean_data['pos_count'] = clean_data['tweetText'].apply(lambda x: pos_sentiment(x))

~\anaconda3\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds) 4198 其他: 4199 值 = self.astype(object)._values -> 4200 映射 = lib.map_infer(values, f, convert=convert_dtype) 4201 4202 if len(mapped) and isinstance(mapped[0], Series):

pandas_libs\lib.pyx in pandas._libs.lib.map_infer()

在 (x) ----> 1 clean_data['pos_count'] = clean_data['tweetText'].apply(lambda x: pos_sentiment(x))

in pos_sentiment(tweet) 2 计数=0 3 用于 pos_list 中的单词: ----> 4 如果推文中的单词: 5 计数+=1 6返回计数

TypeError: 'float' 类型的参数不可迭代

【问题讨论】:

    标签: python function jupyter-notebook anaconda typeerror


    【解决方案1】:

    你正在迭代这一行中的一个浮点数

    clean_data['pos_count'] = clean_data['tweetText'].apply(lambda x: pos_sentiment(x))
    

    因为如果你进入函数 pos_sentiment(x),它应该接受一个列表而不是一个浮点数

    你应该做的是:

    clean_data['pos_count'] = pd.Series(pos_sentiment(clean_data['tweetText'].tolist()))
    

    【讨论】:

    • 感谢 Mohammed 回复,但没有与函数相关的输出
    猜你喜欢
    • 2018-04-13
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多