Python:Pandas Dataframe AttributeError: 'numpy.ndarray' 对象没有属性 'fillna'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37624903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:40:23  来源:igfitidea点击:

Python: Pandas Dataframe AttributeError: 'numpy.ndarray' object has no attribute 'fillna'

pythonpandasdataframeattributeerror

提问by jeangelj

Since I am creating a dataframe, I don't understand why I am getting an array error.

由于我正在创建一个数据框,我不明白为什么会出现数组错误。

M2 = df.groupby(['song_id', 'user_id']).rating.mean().unstack()
M2 = np.maximum(-1, (M - 3).fillna(0) / 2.)  # scale to -1..+1  (treat "0" scores as "1" scores)
M2.head(2)

AttributeError: 'numpy.ndarray' object has no attribute 'fillna'

采纳答案by piRSquared

(M - 3)is getting interpreted as a numpy.ndarray. This implies that Mis defined somewhere as a numpy.ndarray. Test it out by running:

(M - 3)被解释为numpy.ndarray. 这意味着在M某处定义为 a numpy.ndarray。通过运行来测试它:

print type(M)

回答by tmthydvnprt

Your code is not complete at the moment, so it is hard to pin point why Mis causing an error. There could be a couple reasons:

您的代码目前不完整,因此很难确定M导致错误的原因。可能有几个原因:

  1. You have a typo and (M - 3)should be (M2 - 3)

    M2 = df.groupby(['song_id', 'user_id']).rating.mean().unstack()
    M2 = np.maximum(-1, (M2 - 3).fillna(0) / 2.)  # scale to -1..+1  (treat "0" scores as "1" scores)
    M2.head(2)
    
  2. You need to define/convert Mas pandas.DataFramesomewhere else in your code

    # With out seeing this part of the code, no one can really help you
    M = pd.DataFrame(...)
    # ...
    # ...
    M2 = df.groupby(['song_id', 'user_id']).rating.mean().unstack()
    M2 = np.maximum(-1, (M - 3).fillna(0) / 2.)  # scale to -1..+1  (treat "0" scores as "1" scores)
    M2.head(2)
    
  3. You could convert it to a pandas.DataFrameright before you use it.

    M2 = df.groupby(['song_id', 'user_id']).rating.mean().unstack()
    M2 = np.maximum(-1, (pd.DataFrame(M) - 3).fillna(0) / 2.)  # scale to -1..+1  (treat "0" scores as "1" scores)
    M2.head(2)
    
  1. 你有一个错字,(M - 3)应该是(M2 - 3)

    M2 = df.groupby(['song_id', 'user_id']).rating.mean().unstack()
    M2 = np.maximum(-1, (M2 - 3).fillna(0) / 2.)  # scale to -1..+1  (treat "0" scores as "1" scores)
    M2.head(2)
    
  2. 您需要定义/转换Mpandas.DataFrame代码中的其他地方

    # With out seeing this part of the code, no one can really help you
    M = pd.DataFrame(...)
    # ...
    # ...
    M2 = df.groupby(['song_id', 'user_id']).rating.mean().unstack()
    M2 = np.maximum(-1, (M - 3).fillna(0) / 2.)  # scale to -1..+1  (treat "0" scores as "1" scores)
    M2.head(2)
    
  3. 您可以pandas.DataFrame在使用之前将其转换为权利。

    M2 = df.groupby(['song_id', 'user_id']).rating.mean().unstack()
    M2 = np.maximum(-1, (pd.DataFrame(M) - 3).fillna(0) / 2.)  # scale to -1..+1  (treat "0" scores as "1" scores)
    M2.head(2)