Attributeerror: dataframe object has no attribute as_matrix

Encountering errors like attributeerror: dataframe object has no attribute as_matrix is frustrating, but don’t worry, and read through the end of this article to solve your problem.

In this article, we will show you how to solve the error attributeerror: dataframe object has no attribute as_matrix in Python. This error occurs when you attempt to use the as_matrix() function on a Pandas DataFrame object.

It becomes an error due to the fact that the as_matrix() function has been deprecated since version 0.23.0. Anyway, before we begin our tutorial, have a quick overview of Python and AttributeError.

What is Python?

Python is one of the most popular programming languages. It is used for developing a wide range of applications. It is a high-level programming language that is usually used by developers nowadays due to its flexibility.

What is AttributeError?

An attributeerror is an error that appears in our Python codes when we try to access an attribute of a non-existent object. In addition, this occurs when we attempt to perform non-supported operations.

Now that we understand this error and even what Python and an AttributeError are, let’s move on to our “how to fix this error” tutorial.

How to solve “dataframe object has no attribute as_matrix” in Python

To resolve the Python error attributeerror: dataframe object has no attribute as_matrix, you can use the values attribute of the DataFrame in place of the as_matrix() function.

Example of a DataFrame:

import pandas as pd

sample_df = pd.DataFrame({'X': [10, 20, 30], 'Y': [5, 15, 25], 'Z': [3, 6, 9]})
print(sample_df)

Output:

    X   Y  Z
0  10   5  3
1  20  15  6
2  30  25  9

Now, convert the DataFrame to a NumPy array.

Example:

import pandas as pd

sample_df = pd.DataFrame({'X': [10, 20, 30], 'Y': [5, 15, 25], 'Z': [3, 6, 9]})
arr = sample_df.values
print(arr)

Output:

[[10  5  3]
 [20 15  6]
 [30 25  9]]

Another Example

Instead of using this code:

import pandas as pd

sd = {'Name':['Max', 'Sam', 'Drei', 'Elli', 'Cass'],
        'Score':[20, 15, 18, 17, 20]}
s_df = pd.DataFrame(sd)
s_df.head()
arr = s_df.as_matrix()

Error:

Traceback (most recent call last):
File "C:\Users\path\path\path\sample.py", line 6, in
arr = s_df.as_matrix()
^^^^^^^^^^^^^^
File "C:\Users\path\path\path\venv\Lib\site-packages\pandas\core\generic.py", line 5902, in getattr
return object.getattribute(self, name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'DataFrame' object has no attribute 'as_matrix'

Use this code:

import pandas as pd

sd = {'Name':['Max', 'Sam', 'Drei', 'Elli', 'Cass'],
        'Score':[20, 15, 18, 17, 20]}
s_df = pd.DataFrame(sd)
arr = s_df.values
print(arr)

Output:

[['Max' 20]
 ['Sam' 15]
 ['Drei' 18]
 ['Elli' 17]
 ['Cass' 20]]

Conclusion

In conclusion, the Python error attributeerror: dataframe object has no attribute as_matrix can be easily solved by replacing the as_matrix() function with the values attribute of the DataFrame.

By following the guide above, there’s no doubt that you’ll be able to resolve this error quickly and without a hassle.

I think that’s all for today, ITSOURCECODERS! We hope you’ve learned a lot from this. If you have any questions or suggestions, please leave a comment below, and for more attributeerror tutorials in Python, visit our website.

Thank you for reading!

Leave a Comment