代码之家  ›  专栏  ›  技术社区  ›  Tasos

使用带参数的dataframe中的apply返回错误

  •  0
  • Tasos  · 技术社区  · 7 年前

    我有以下代码

    def mean_vectorizer(words, dimention, diction):
      return np.array([np.mean([diction[w] for w in words if w in diction] or [np.zeros(dimention)], axis=0)])
    
    data['vector'] = data.apply(lambda x: mean_vectorizer(x['words'], 100, diction), axis=1)
    

    words diction

    运行上述代码将返回以下错误:

    Exception: Data must be 1-dimensional         
    

    但是,如果我用下面的代码运行一行代码,它不会抱怨

    words = data.iloc[0]['words']
    np.array([np.mean([diction[w] for w in words if w in diction] or [np.zeros(dimention)], axis=0)])
    
    0 回复  |  直到 7 年前