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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:38:14  来源:igfitidea点击:

AttributeError: 'DataFrame' object has no attribute 'Height'

python-2.7pandas

提问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.columnsreturns 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.

试试看,它会起作用。