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.
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.
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