Python “模块”对象没有属性“DataFrame”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20621607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'module' object has no attribute 'DataFrame'
提问by aerijman
For the following code:
对于以下代码:
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:
我收到错误:
'module' object has no attribute 'DataFrame'.
Any ideas on what is happening and how to fix this problem?
关于发生了什么以及如何解决这个问题的任何想法?
回答by Michael Gruen
The most likely explanation is that either a file called 'pandas.py' is in the same directory as your script, or that another variable called 'pd' is used in your program.
最可能的解释是,一个名为“pandas.py”的文件与您的脚本位于同一目录中,或者您的程序中使用了另一个名为“pd”的变量。
回答by Daniel Klaus
The code presented here doesn't show this discrepancy, but sometimes I get stuck when invoking dataframein all lower case.
此处提供的代码没有显示这种差异,但有时我dataframe在以全部小写形式调用时会卡住。
Switching to camel-case (pd.DataFrame()) cleans up the problem.
切换到驼峰式 ( pd.DataFrame()) 可以解决问题。
回答by SVK
I have faced similar problem, 'int' object has no attribute 'DataFrame',
我遇到过类似的问题,'int' 对象没有属性 'DataFrame',
This was because i have mistakenly used pd as a variable in my code and assigned an integer to it, while using the same pd as my pandas dataframe object by declaring - import pandas as pd.
这是因为我错误地将 pd 用作代码中的变量并为其分配了一个整数,同时通过声明使用了与我的熊猫数据帧对象相同的 pd - import pandas as pd.
I realized this, and changed my variable to something else, and fixed the error.
我意识到这一点,并将我的变量更改为其他内容,并修复了错误。
回答by Sandy
Please make sure that your file name should not be panda.pyor pd.py.
Also, make sure that panda is there in your Lib/site-packagesdirectory, if not that you need to install panda using below command line:
请确保您的文件名不应该是panda.py或pd.py。另外,请确保您的Lib/site-packages目录中存在熊猫,否则您需要使用以下命令行安装熊猫:
pip install pandas
if you work with proxy then try calling below in command prompt:
如果您使用代理,请尝试在命令提示符下调用以下命令:
python.exe -m pip install pandas --proxy="YOUR_PROXY_IP:PORT"
回答by Hans
For me he problem was that my script was called pandas.pyin the folder pandaswhich obviously messed up my imports.
对我来说,他的问题是我的脚本pandas.py在文件夹中被调用,pandas这显然弄乱了我的导入。
回答by raju
Change the file name if your file name is like pandas.py or pd.py, it will shadow the real name otherwise.
如果您的文件名类似于 pandas.py 或 pd.py,请更改文件名,否则它将隐藏真实名称。
回答by Jeff
I recieved a similar error:
我收到了类似的错误:
AttributeError: module 'pandas' has no attribute 'DataFrame'
AttributeError: 模块“pandas”没有属性“DataFrame”
The cause of my error was that I ran pip install of pandas as root, and my user did not have permission to the directory.
我的错误原因是我以 root 身份运行了 Pandas 的 pip install,而我的用户没有该目录的权限。
My fix was to run:
我的修复是运行:
sudo chmod -R 755 /usr/local/lib/python3.6/site-packages
须藤 chmod -R 755 /usr/local/lib/python3.6/site-packages
回答by Bhaskar Jyoti Das
There may be two causes:
可能有两个原因:
It is case-sensitive: DataFrame .... Dataframe, dataframe will not work.
You have not install pandas (
pip install pandas) in the python path.
它区分大小写:DataFrame .... Dataframe,dataframe 将不起作用。
您尚未
pip install pandas在 python 路径中安装 pandas ( )。

