Python: Pandas Dataframe AttributeError: 'numpy.ndarray' object has no attribute 'fillna'
(M - 3)
is getting interpreted as a numpy.ndarray
. This implies that M
is defined somewhere as a numpy.ndarray
. Test it out by running:
print type(M)
Your code is not complete at the moment, so it is hard to pin point why M
is causing an error. There could be a couple reasons:
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)
You need to define/convert
M
aspandas.DataFrame
somewhere 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)
You could convert it to a
pandas.DataFrame
right 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)
You are calling the .fillna()
method on a numpy array. And numpy
arrays don't have that method defined.
You can probably convert the numpy
array to a pandas.DataFrame
and then apply the .fillna()
method.