'numpy.ndarray' object has no attribute 'fillna'
时间: 2023-09-23 11:11:35 浏览: 184
The error message indicates that you are trying to call the `fillna` method on a NumPy array, but that method is not defined for NumPy arrays.
The `fillna` method is a method of Pandas DataFrame and Series objects, not NumPy arrays. If you want to fill missing values in a NumPy array, you can use the `numpy.nan_to_num` function to replace NaN values with a specified value, or the `numpy.ma.masked_invalid` function to mask invalid (NaN or infinite) values.
For example:
```
import numpy as np
a = np.array([1, 2, np.nan, 4])
# Replace NaN values with 0
a = np.nan_to_num(a, nan=0)
# Mask invalid values
a = np.ma.masked_invalid(a)
```
Alternatively, you can convert the NumPy array to a Pandas DataFrame or Series object, and then use the `fillna` method:
```
import pandas as pd
import numpy as np
a = np.array([1, 2, np.nan, 4])
# Convert to Pandas Series object
s = pd.Series(a)
# Fill NaN values with 0
s = s.fillna(0)
# Convert back to NumPy array
a = s.values
```
阅读全文