'numpy.float64' object has no attribute 'apply'
时间: 2023-10-30 08:07:46 浏览: 302
The error message "'numpy.float64' object has no attribute 'apply'" suggests that you are trying to call the 'apply' method on a NumPy float64 object, which is not supported. The 'apply' method is typically used with pandas DataFrames or Series objects, not with individual scalar values.
If you are trying to apply a function to a NumPy array or a pandas DataFrame, you can use the 'apply' function from the pandas library. Here is an example of how you can use it:
```python
import pandas as pd
# Create a pandas DataFrame with a column of NumPy float64 values
df = pd.DataFrame({'values': [1.2, 3.4, 5.6]})
# Define a function to apply to each value in the DataFrame
def square(x):
return x ** 2
# Use the 'apply' function to apply the 'square' function to each value in the 'values' column
df['squared_values'] = df['values'].apply(square)
print(df)
```
This will square each value in the 'values' column and store the result in a new column called 'squared_values' in the DataFrame.
阅读全文