For the following code:
import pandas as pd df = pd.DataFrame(np.random.rand(12,2), columns=['Apples', 'Oranges'] ) df['Categories'] = pd.Series(list('AAAABBBBCCCC')) pd.options.display.mpl_style = 'default' df.boxplot(by='Categories')
I get the error:
'pandas' object has no attribute 'DataFrame'.
Any ideas on what is happening and how to fix this problem?
If you have named the script as pd.py or pandas.py then you will get module 'pandas' has no attribute 'dataframe' error. This mainly happens because the file name will shadow the Pandas module and, it can mess up the module imports. We can fix this issue by renaming the script to some other name such as “my_script.py”.
The most likely cause of the error is having a local file named pandas.py which shadows the official pandas module. Make sure you haven't misspelled Series as class names are case-sensitive. Make sure to rename your local file to something other than pandas.py to solve the error.
AttributeError can be defined as an error that is raised when an attribute reference or assignment fails. For example, if we take a variable x we are assigned a value of 10. In this process suppose we want to append another value to that variable. It's not possible.
Series append syntax The syntax for using append on a Series is very similar to the dataframe syntax. You type the name of the first Series, and then . append() to call the method. Then inside the parenthesis, you type the name of the second Series, which you want to append to the end of the first.
The code presented here doesn't show this discrepancy, but sometimes I get stuck when invoking dataframe
in all lower case.
Switching to camel-case (pd.DataFrame()
) cleans up the problem.
Please check if:
a) you've named a file 'pandas.py' in the same directory as your script, or
b) another variable called 'pd' is used in your program.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With