Python 数据框对象没有属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38134643/
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
Data-frame Object has no Attribute
提问by Mouna Belaid
I know that this kind of question was asked before and I've checked all the answers and I have tried several times to find a solution but in vain. In fact I call a Dataframe using Pandas. I've uploaded a csv.file.
我知道以前有人问过这种问题,我已经检查了所有答案,并且尝试了几次以找到解决方案,但徒劳无功。事实上,我使用 Pandas 调用 Dataframe。我上传了一个 csv.file。
When I type data.Country
and data.Year
, I get the 1st Column and the second one displayed. However when I type data.Number
, everytime it gives me this error:
当我输入data.Country
and 时data.Year
,会显示第一列和第二列。但是,当我输入时data.Number
,每次它都会给我这个错误:
AttributeError: 'DataFrame' object has no attribute 'Number'.
AttributeError: 'DataFrame' 对象没有属性 'Number'。
回答by Merlin
Check your DataFrame with data.columns
检查您的数据帧 data.columns
It should print something like this
它应该打印这样的东西
Index([u'regiment', u'company', u'name',u'postTestScore'], dtype='object')
Check for hidden white spaces..Then you can rename with
检查隐藏的空格..然后你可以重命名
data = data.rename(columns={'Number ': 'Number'})
回答by piRSquared
I'm going to take a guess. I think the column name that contains "Number"
is something like " Number"
or "Number "
. Notice that I'm assuming you might have a residual space in the column name somewhere. Do me a favor and run print "<{}>".format(data.columns[1])
and see what you get. Is it something like < Number>
? If so, then my guess was correct. You should be able to fix it with this:
我来猜一猜。我认为包含的列名"Number"
类似于" Number"
or "Number "
。请注意,我假设您在某处的列名中可能有剩余空间。帮我一个忙,然后跑去print "<{}>".format(data.columns[1])
看看你会得到什么。它是这样的< Number>
吗?如果是这样,那么我的猜测是正确的。您应该可以通过以下方式修复它:
data.columns = data.columns.str.strip()
回答by Sherman Kasaval
Quick fix: Change how excel converts imported files. Go to 'File', then 'Options', then 'Advanced'. Scroll down and uncheck 'Use system seperators'. Also change 'Decimal separator' to '.' and 'Thousands separator' to ',' . Then simply 're-save' your file in the CSV (Comma delimited) format. The root cause is usually associated with how the csv file is created. Trust that helps. Point is, why use extra code if not necessary? Cross-platform understanding and integration is key in engineering/development.
快速修复:更改 excel 转换导入文件的方式。转到“文件”,然后是“选项”,然后是“高级”。向下滚动并取消选中“使用系统分隔符”。还将“十进制分隔符”更改为“.” 和 '千位分隔符' 到 ',' 。然后只需以 CSV(逗号分隔)格式“重新保存”您的文件。根本原因通常与 csv 文件的创建方式有关。相信有帮助。重点是,如果不需要,为什么要使用额外的代码?跨平台理解和集成是工程/开发的关键。
回答by Ajay Raj
I'd like to make it simple for you. the reason of " 'DataFrame' object has no attribute 'Number'/'Close'/or any col name " is because you are looking at the col name and it seems to be "Number" but in reality it is " Number" or "Number " , that extra space is because in the excel sheet col name is written in that format. You can change it in excel or you can write data.columns = data.columns.str.strip() / df.columns = df.columns.str.strip() but the chances are that it will throw the same error in particular in some cases after the query. changing name in excel sheet will work definitely.
我想为你简单点。“'DataFrame' 对象没有属性 'Number'/'Close'/ 或任何列名称”的原因是因为您正在查看列名称,它似乎是“Number”,但实际上它是“Number”或"Number" ,额外的空间是因为在 Excel 表格中 col 名称是以该格式写入的。您可以在 excel 中更改它,也可以编写 data.columns = data.columns.str.strip() / df.columns = df.columns.str.strip() 但很有可能它会特别抛出相同的错误在某些情况下,在查询之后。在excel表中更改名称肯定会起作用。