Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

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
like image 427
user3062459 Avatar asked Jan 27 '15 04:01

user3062459


People also ask

How do you solve a DataFrame object has no attribute?

If you try to call concat() on a DataFrame object, you will raise the AttributeError: 'DataFrame' object has no attribute 'concat'. You have to pass the columns to concatenate to pandas. concat() and define the axis to concatenate along.

Which is not an attribute of DataFrame object?

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.

What is a DataFrame object?

DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. You can think of it like a spreadsheet or SQL table, or a dict of Series objects. It is generally the most commonly used pandas object.

How large can data frames be?

The long answer is the size limit for pandas DataFrames is 100 gigabytes (GB) of memory instead of a set number of cells.


2 Answers

I have run into a similar issue before when reading from csv. Assuming it is the same:

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.

like image 88
JAB Avatar answered Oct 21 '22 15:10

JAB


PS On above answer by JAB - if there is clearly spaces in your column names use skipinitialspace=True in read_csv e.g.

df = pd.read_csv('/path../NavieBayes.csv',skipinitialspace=True)

like image 30
Hamish Robertson Avatar answered Oct 21 '22 14:10

Hamish Robertson