pandas AttributeError: 'DataFrame' 对象没有属性 'Height'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28163439/
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
AttributeError: 'DataFrame' object has no attribute 'Height'
提问by user3062459
I am able to convert a csv file to pandas DataFormat and able to print out the table, as seen below. However, when I try to print out the Height column I get an error. How can I fix this?
我能够将 csv 文件转换为 pandas DataFormat 并能够打印出表格,如下所示。但是,当我尝试打印高度列时,出现错误。我怎样才能解决这个问题?
import pandas as pd
df = pd.read_csv('/path../NavieBayes.csv')
print df #this prints out as seen below
print df.Height #this gives me the "AttributeError: 'DataFrame' object has no attribute 'Height'
Height Weight Classifer
0 70.0 180 Adult
1 58.0 109 Adult
2 59.0 111 Adult
3 60.0 113 Adult
4 61.0 115 Adult
回答by JAB
I have run into a similar issue before when reading from csv
. Assuming it is the same:
我之前在从csv
. 假设是一样的:
col_name =df.columns[0]
df=df.rename(columns = {col_name:'new_name'})
The error in my case was caused by (I think) by a byte order marker in the csv or some other non-printing character being added to the first column label. df.columns
returns an array of the column names. df.columns[0]
gets the first one. Try printing it and seeing if something is odd with the results.
我的情况下的错误是由(我认为)由 csv 中的字节顺序标记或其他一些非打印字符添加到第一列标签引起的。df.columns
返回一个列名数组。df.columns[0]
得到第一个。尝试打印它,看看结果是否有什么奇怪的地方。
回答by Hamish Robertson
PS On above answer by JAB - if there is clearly spaces in your column names use skipinitialspace=True in read_csv e.g.
PS 在 JAB 的上述回答中 - 如果您的列名中有明显的空格,则在 read_csv 中使用 skipinitialspace=True 例如
df = pd.read_csv('/path../NavieBayes.csv',skipinitialspace=True)
df = pd.read_csv('/path../NavieBayes.csv',skipinitialspace=True)
回答by tulsi kumar
df = pd.read_csv(r'path_of_file\csv_file_name.csv')
OR
或者
df = pd.read_csv('path_of_file/csv_file_name.csv')
Example:
例子:
data = pd.read_csv(r'F:\Desktop\datasets\hackathon+data+set.csv')
Try it, it will work.
试试看,它会起作用。