'numpy.ndarray' object has no attribute 'applymap'
时间: 2023-10-30 16:59:19 浏览: 87
The 'numpy.ndarray' object does not have an 'applymap' attribute because the 'applymap' function is specific to pandas DataFrames and Series objects, not numpy arrays.
If you want to apply a function element-wise to a numpy array, you can use the 'numpy.vectorize' function or a loop to achieve this. Here's an example using 'numpy.vectorize':
```python
import numpy as np
# Define a sample function
def square(x):
return x**2
# Create a numpy array
arr = np.array([1, 2, 3, 4, 5])
# Create a vectorized version of the function
vec_square = np.vectorize(square)
# Apply the function element-wise to the array
result = vec_square(arr)
print(result)
```
Output:
```
[ 1 4 9 16 25]
```
In this example, the 'square' function is applied element-wise to the 'arr' numpy array using 'np.vectorize'. The resulting array 'result' contains the squared values of each element in 'arr'.
阅读全文