Python 数据框对象没有属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34903082/
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 Tinkinc
I am trying to call Dataframe columns for analysis using Pandas. I uploaded a CSV file, however every time It gives me this error AttributeError: 'DataFrame' object has no attribute 'X'
How can I make every column available for analysis and why does this always happen.
我正在尝试使用 Pandas 调用 Dataframe 列进行分析。我上传了一个 CSV 文件,但是每次它都会给我这个错误AttributeError: 'DataFrame' object has no attribute 'X'
如何使每一列都可用于分析以及为什么总是发生这种情况。
proportion_women_survived = float(sum(women.survived))/len(women)
This is one example. I read_CSV('Train.csv)
into Python editor and the columns are shown when I data.head()
however not one column unless I do this:
proportion_women_survived = float(sum(women.survived))/len(women)
这是一个例子。我read_CSV('Train.csv)
进入 Python 编辑器,但当我data.head()
不显示一列时会显示列,除非我这样做:
[{newCols = data.columns.values
newCols[-1] = 'PassengerId'
data.columns = newCols}]
回答by EdChum
You get the error because your column names are case-sensitive, typically you can check what your columns really are by using df.columns.tolist()
as you're concerned about this you can lower case the columns after loading by using:
您收到错误是因为您的列名区分大小写,通常您可以通过使用来检查您的列到底是什么,df.columns.tolist()
因为您担心这一点,您可以使用以下方法在加载后小写列:
df.columns = df.columns.str.lower()
Example:
例子:
In [203]:
df = pd.DataFrame(columns=list('aBCd'))
df
Out[203]:
Empty DataFrame
Columns: [a, B, C, d]
Index: []
In [204]:
df.columns.str.lower()
Out[204]:
Index(['a', 'b', 'c', 'd'], dtype='object')