Python Pandas

How to Fix module ‘pandas’ has no attribute ‘dataframe’

In pandas, one of the ways to create a DataFrame is by using the DataFrame() constructor, which should be in camel case. If you specify it in any other case or with incorrect spelling, it generates the AttributeError: module ‘pandas’ has no attribute ‘dataframe.’ Let’s reproduce this error and resolve this error in this guide with an example. Also, we will look at the AttributeError: module ‘pandas’ has no attribute ‘DataFrame’.

Error Scenario: AttributeError: module ‘pandas’ has no attribute ‘dataframe’

Try to create a DataFrame, campaign_details, using pandas.dataframe() constructor with three rows and three columns.

import pandas

campaign_details=pandas.dataframe([['Marketing','USA',25000],
                                   ['Sales','India',15000],
                                   ['Technical','Italy',20000]],columns=['Name','Location','Budget'],index=['C1','C2','C3'])

print(campaign_details)

Output

You can see that AttributeError is raised.

Solution

Specify the pandas.DataFrame() constructor to create a DataFrame with three rows and three columns.

import pandas

campaign_details=pandas.DataFrame([['Marketing','USA',25000],
                                   ['Sales','India',15000],
                                   ['Technical','Italy',20000]],columns=['Name','Location','Budget'],index=['C1','C2','C3'])

print(campaign_details)

Output

Now, the campaign_details DataFrame has been created without any AttributeError.

Error Scenario: AttributeError: module ‘pandas’ has no attribute ‘DataFrame’

If the code is saved as pandas.py or pd.py, it raises an AttributeError: module ‘pandas’ has no attribute ‘DataFrame.’ Let’s reproduce this error first. Create the same DataFrame and save the Python file as pandas.py

Output

You can see that an AttributeError is raised.

Solution

Let’s save the file with a different name: ‘camps.py’

Output

Now, the campaign_details DataFrame has been created without any AttributeError.

Conclusion

Now, we are able to fix the Attribute Error – ‘pandas’ has no attribute dataframe by specifying the constructor in camel case when creating the pandas DataFrame. Also, we observed the Attribute Error – ‘pandas’ has no attribute ‘DataFrame’ if the file is saved with pandas.py. We resolved this error by saving the Python file with a different name, avoiding pandas.py or pd.py

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain