46

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'.

Fabich
  • 2,496
  • 3
  • 28
  • 39
Mouna Belaid
  • 503
  • 1
  • 5
  • 9
  • I mean I get the 1st column and the 3rd one displayed – Mouna Belaid Jun 30 '16 at 23:07
  • 4
    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. – Jeff Jun 30 '16 at 23:12
  • try `skipinitialspace = True` and `quotechar=' " '` while reading the csv file as its hard to say without the file. – shivsn Jul 01 '16 at 18:56

6 Answers6

57

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'})
Merlin
  • 22,195
  • 35
  • 117
  • 197
  • 1
    Let me give you my favorite tutorial, enjoy! https://people.duke.edu/~ccc14/sta-663/UsingPandas.html – Merlin Jul 01 '16 at 19:55
21

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()
piRSquared
  • 265,629
  • 48
  • 427
  • 571
4
data = pd.read_csv('/your file name', delim_whitespace=True)
data.Number

now you can run this code with no error.

zealous
  • 7,088
  • 3
  • 11
  • 35
  • 3
    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. – Rajan Jun 10 '20 at 06:51
3

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.

0

I'd like to make it simple for you. 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. You can change it in excel or you can write data.columns = data.columns.str.strip() / df.columns = df.columns.str.strip() but the chances are that it will throw the same error in particular in some cases after the query. changing name in excel sheet will work definitely.

0

Change ";" for "," in the csv file