Attributeerror: ‘numpy.ndarray’ object has no attribute ‘columns’

In this article, we’ll explore the solution for attributeerror: ‘numpy.ndarray’ object has no attribute ‘columns’ error message. This error message specifically occurs when you are working with data analysis in Python.

Unfortunately, this error message can be confusing if you don’t know how to fix it and frustrating, especially if you are new to Python programming.

You must fix this ‘numpy.ndarray’ object has no attribute ‘columns’ error in order to run your code perfectly.

What is attributeerror: ‘numpy.ndarray’ object has no attribute ‘columns’ error?

'numpy.ndarray' object has no attribute 'columns' error

The “attributeError: numpy.ndarray’ object has no attribute ‘columns'” error message occurs when you are trying to access the columns attribute of a NumPy array. This doesn’t exist because NumPy arrays don’t have columns attribute like Pandas dataframes.

In addition to that, this error can happen when you mistakenly assume that a NumPy array has the same attributes as a Pandas DataFrame have.

If you are not familiar with Pandas DataFrame, the ‘columns’ attribute is a specific attribute of a Pandas DataFrame that contains the names of the columns, but it does not exist in NumPy arrays.

How to fix attributeerror: ‘numpy.ndarray’ object has no attribute ‘columns’ error?

To fix this error, you have to make sure that you are using the correct data structure and accessing the correct attribute for that data structure.

Furthermore, you should create it using the pd.DataFrame() function with your data as input rather than trying to access the ‘columns’ attribute of a NumPy array.

If you only need a NumPy array, you can simply continue to work with that array and avoid trying to access DataFrame-specific attributes like ‘columns’.

Solution for “attributeerror: ‘numpy.ndarray’ object has no attribute ‘columns’ error

Here’s the solution you may use to fix the error that you are facing right now.

Before we convert the numpy array first, we are going to show you why your code results in an error.

Example code 1:

import numpy as np

# Create a Numpy ndarray
my_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Try to access the 'columns' attribute of the Numpy ndarray
print(my_array.columns)

Output:

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

Explanation:

In this example, as you can see in the code above, we are trying to access the ‘columns’ attribute of a Numpy ndarray, which doesn’t have this attribute. As a result, it will throw an error message.

Example code 2:

import numpy as np
import pandas as pd

# Create a Numpy ndarray
my_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Convert the Numpy ndarray to a Pandas DataFrame
df = pd.DataFrame(my_array)

# Try to access the 'columns' attribute of the Numpy ndarray
print(my_array.columns)

Output:

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

Explanation:

In this example, we already converted the Numpy array to a Pandas DataFrame using the pd.DataFrame function.

However, as you can see in the above code, we are still trying to access the ‘columns’ attribute of the Numpy ndarray using my_array.columns. As a result, it will throw an error message.

Convert a numpy array to a Pandas DataFrame

Now, it’s time to convert the code into a Pandas DataFrame.

import numpy as np
import pandas as pd

# Create a Numpy ndarray
my_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Convert the Numpy ndarray to a Pandas DataFrame
df = pd.DataFrame(my_array)

# Access the 'columns' attribute of the Pandas DataFrame
print(df.columns)

Output:

Finally, we completely converted a numpy array to a Pandas DataFrame.

Index([0, 1, 2], dtype='object')

Example 1:

import numpy as np
import pandas as pd

# create a numpy array
my_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# convert the numpy array to a Pandas DataFrame
my_dataframe = pd.DataFrame(my_array, columns=['X', 'Y', 'Z'])

# access the 'columns' attribute of the Pandas DataFrame
print(my_dataframe.columns)

Output:

Index(['X', 'Y', 'Z'], dtype='object')

Example 2:

import numpy as np
import pandas as pd

# Create a Numpy ndarray
my_array = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 100]])

# Convert the Numpy ndarray to a Pandas DataFrame with column names
df = pd.DataFrame(my_array, columns=['col1', 'col2', 'col3'])

# Access the 'columns' attribute of the Pandas DataFrame
print(df.columns)

Output:

Index(['col1', 'col2', 'col3'], dtype='object')

Related Articles for Python Errors

Conclusion

Now you can easily fix the error because this article provides solutions for theattributeerror: ‘numpy.ndarray‘”object has no attribute ‘columns’“, which is a big help in solving the error that you are currently facing.

The error message indicates that the columns attribute doesn’t exist in a NumPy array because NumPy arrays don’t have columns attributes like Pandas dataframes.

Take note that columns attribute only exists in Pandas DataFrame.

We are really hoping that this article will totally help you troubleshoot this numpy.ndarray’ object has no attribute ‘columns’ error.

Thank you very much for reading to the end of this article. Just in case you have more questions or inquiries, feel free to comment, and you can also visit our website for additional information.

Leave a Comment