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:

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

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!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!