Python:AttributeError ‘numpy.ndarray’ object has no attribute ‘fillna’

Published on:
Last updated:

This post is also available in: Japanese

This is an error often encountered while doing data analysis such as machine learning using Python,Numpy,Pandas.
I often forget, so I am writing this post as a memo.

I think it is more easy to use 'fillna'(not to use 'drop') in order to keep the number of data with train_y unchanged.
However, since it affects the results of data analysis, you need to pay attention to the data to be replaced.

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

Replace missing values(Nan) with previous values

The following is a sample when you want to replace missing values(Nan) with previous values.

# Replace missing values with previous values
train_X = train_df.fillna(method='ffill').values

Replace missing values(Nan) with next values

The following is a sample when you want to replace missing values(NaN) with next values.

# Replace missing values with next values
train_X = train_df.fillna(method='bfill').values

Delete a column with missing values

The following is a sample when you want to delete a column with missing values.

# Delete a column with missing values
train_X = train_df.drop(train_df.columns[np.isnan(train_df).any()], axis=1).values

About
Kuniyoshi Takemoto is the founder of Amelt.net LLC, and editor of this blog(www.amelt.net).Learn more and follow me on LinkedIn.