Data-frame Object has no Attribute

310,504

Solution 1

Check your DataFrame with 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'})

Solution 2

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:

data.columns = data.columns.str.strip()

Solution 3

data = pd.read_csv('/your file name', delim_whitespace=True)
data.Number

now you can run this code with no error.

Solution 4

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.

Share:
310,504

Related videos on Youtube

Mouna Belaid
Author by

Mouna Belaid

Updated on January 14, 2022

Comments

  • Mouna Belaid
    Mouna Belaid over 2 years

    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.

    dataset from csv

    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:

    AttributeError: 'DataFrame' object has no attribute 'Number'.

    • Jeff
      Jeff almost 8 years
      It's hard to say without being able to load your data. Did you try using data['Number'] to access that column? Type in df.columns.values to see an array of your column names.
  • Merlin
    Merlin almost 8 years
    Let me give you my favorite tutorial, enjoy! people.duke.edu/~ccc14/sta-663/UsingPandas.html
  • Shanteshwar Inde
    Shanteshwar Inde about 5 years
    Hi, welcome to Stackoverflow!! It would be great if you could read these guidelines before answering any question. Thanks.
  • Rajan
    Rajan almost 4 years
    When possible, please make an effort to provide additional explanation instead of just code. Such answers tend to be more useful as they help members of the community and especially new developers better understand the reasoning of the solution, and can help prevent the need to address follow-up questions.
  • Ilan Aizelman WS
    Ilan Aizelman WS over 2 years
    Worked like a charm! Thanks a lot.

Related