How to Solve Python AttributeError: ‘numpy.ndarray’ object has no attribute ‘apply’

by | Programming, Python, Tips

If you attempt to call the apply() method on a NumPy array, you will raise the AttributeError: ‘numpy.ndarray’ object has no attribute ‘apply’. The apply() method is a DataFrame and a Series method. This error typically occurs when you call values on a DataFrame or a Series, which returns a numpy.ndarray and then try to call apply on this object.

You can solve this error by not calling the values method and calling apply directly on the DataFrame or the Series.

This tutorial will go through the error in detail and how to solve it with code examples.


AttributeError: ‘numpy.ndarray’ object has no attribute ‘apply’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The apply method is a DataFrame and Series method, that invokes a function on the values of a Series or along an axis of the DataFrame.

Example

Consider the following example of a DataFrame.

import numpy as np

import pandas as pd

df = pd.DataFrame({'x': np.random.randint(0, 100, size=20)

print(df)
    x
0   81
1   24
2   87
3   74
4   63
5   87
6   17
7   43
8   95
9   39
10  75
11  81
12  24
13  55
14  85
15  46
16  18
17  95
18  31
19  83

We want to multiply the values in the x column by 3 and assign these values to a new column in the DataFrame.

values = df['x'].values
df['x_3'] = values.apply(lambda x : x * 3)

Let’s run the code to see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [8], in <cell line: 2>()
      1 values = df['x'].values
----> 2 df['x_3'] = values.apply(lambda x : x * 3)

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

The error occurs because values returns a NumPy representation of a DataFrame or Series. We can verify that the values method returns a numpy.ndarray by using the built-in type() method as follows:

print(type(df['x']))

print(type(df['x'].values))
<class 'pandas.core.series.Series'>
<class 'numpy.ndarray'>

Solution #1

We can solve the error by not using values at all and calling apply directly on the column of the DataFrame. Let’s look at the revised code:

df['x_3'] = df['x'].apply(lambda x : x * 3)

print(df)

Let’s run the code to see the result:

     x  x_3
0   81  243
1   24   72
2   87  261
3   74  222
4   63  189
5   87  261
6   17   51
7   43  129
8   95  285
9   39  117
10  75  225
11  81  243
12  24   72
13  55  165
14  85  255
15  46  138
16  18   54
17  95  285
18  31   93
19  83  249

We successfully created a second column that contains the first column values multiplied by 3.

Solution #2

We can perform the multiplication operation on entire columns by using the * operator. However, this approach is slower than the apply() method. Let’s look at the revised code:

df['x_3'] = df['x'] * 3

print(df)

Let’s run the code to see the result:

     x  x_3
0   81  243
1   24   72
2   87  261
3   74  222
4   63  189
5   87  261
6   17   51
7   43  129
8   95  285
9   39  117
10  75  225
11  81  243
12  24   72
13  55  165
14  85  255
15  46  138
16  18   54
17  95  285
18  31   93
19  83  249

We successfully created a second column that contains the first column values multiplied by 3.

Summary

Congratulations on reading to the end of this tutorial! You can ensure that the object you are handling is not a numpy.ndarray by using the built-in type() method. Remember that calling values on a Series or DataFrame returns a numpy.ndarray.

For further reading on AttributeErrors involving NumPy ndarrays, go to the article:

Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee