DataFrame Object Has No Attribute As_Matrix

5 mins read

Last Updated on September 16, 2022

Quick Answer
df.as_matrix() was deprecated after version 0.23.0. Use df.values instead.
You may convert a DataFrame to its NumPy array representation by using the following DataFrame.values() or DataFrame.to_numpy

Python Pandas errors are often accompanied by the ‘DataFrame object has no attribute as_matrix’. This article explores the deprecation of df.as_matrix() and other similar functions in Numpy. Read on for the latest solution! This article is updated as of Python 0.24.0. It is also worth reading if you want to avoid running into this error.

Python Pandas error: ‘DataFrame’ object has no attribute as_matrix

This error is caused by a missing attribute, as_matrix. You can use the to_numpy method instead of the as_matrix method in Python. This error is not specific to Pandas, but rather to dataframes. It happens because a DataFrame object has no attribute called ‘as_matrix’.

The ‘DataFrame’ object must have an attribute called ‘as_matrix’. This attribute is used for calculating the number of columns in a matrix. It has a name similar to ‘as_matrix’ in Microsoft Excel. If you want to use the as_matrix attribute, you must specify ‘as_matrix’ when creating the DataFrame object.

df.as_matrix() deprecated

DataFrame.as_matrix() is deprecated since version 0.23.0, so it is no longer available. You can use the to_numpy() method instead. As the name implies, this method creates an array of lists with a non-jagged structure. It is suitable for data sets that have four or more values in each unique batch.

Example

Let’s look at an example where we want to convert a DataFrame to a NumPy array. We will start with a CSV file containing pizza names and prices. We will save the file as

pizzas.csv.

pizza,price
margherita,£7.99
pepperoni,£8.99
four cheeses,£10.99
funghi,£8.99
Next, we will load the data into a DataFrame using pandas.

import pandas as pd
df = pd.read_csv(‘pizzas.csv’)
print(df)
pizza price
0 margherita £7.99
1 pepperoni £8.99
2 four cheeses £10.99
3 funghi £8.99
We want the pizza prices to be floating-point numbers instead of strings. We will use the string accessor method to remove the £ character and then cast the column to float using astype().

df.price = df.price.str.replace(‘£’,”).astype(float)
print(df.price)
0 7.99
1 8.99
2 10.99
3 8.99
Name: price, dtype: float64
Then we will try to convert the DataFrame to a NumPy array using as_matrix.

arr = df.as_matrix()
print(arr)
—————————————————————————
AttributeError Traceback (most recent call last)
Input In [23], in <cell line: 1>()
—-> 1 arr = df.as_matrix()
2 print(arr)
File ~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/generic.py:5583, in NDFrame.__getattr__(self, name)
5576 if (
5577 name not in self._internal_names_set
5578 and name not in self._metadata
5579 and name not in self._accessors
5580 and self._info_axis._can_hold_identifiers_and_holds_name(name)
5581 ):
5582 return self[name]
-> 5583 return object.__getattribute__(self, name)
AttributeError: ‘DataFrame’ object has no attribute ‘as_matrix’
The error occurs because as_matrix() is a deprecated method.

Numpy Series.as_matrix() is deprecated

In Numpy, a Series object is a one-dimensional, labeled array. A Series object may have either integer or label-based indexing. The series object can be created by using the pandas series function, which converts a Series object to a Numpy-array. It returns all columns or the specified columns, depending on the caller’s argument.

It is an error when a Series object has no attribute ‘as_matrix’. The dataframe object is not a Numpy Series object. It has the prefix str, list, dict, or None. Alternatively, you can use the to_numpy() method instead. Using the values attribute instead of as_matrix() will result in an AttributeError.

About The Author

Pat Rowse is a thinker. He loves delving into Twitter to find the latest scholarly debates and then analyzing them from every possible perspective. He's an introvert who really enjoys spending time alone reading about history and influential people. Pat also has a deep love of the internet and all things digital; she considers himself an amateur internet maven. When he's not buried in a book or online, he can be found hardcore analyzing anything and everything that comes his way.