Data-frame Object has no Attribute

前端 未结 5 1094
我寻月下人不归
我寻月下人不归 2020-12-13 18:26

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 Datafra

相关标签:
5条回答
  • 2020-12-13 19:01

    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()
    
    0 讨论(0)
  • 2020-12-13 19:04

    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 讨论(0)
  • 2020-12-13 19:11

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

    data.Number

    now you can run this code with no error.

    0 讨论(0)
  • 2020-12-13 19:13

    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 讨论(0)
  • 2020-12-13 19:18

    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'})
    
    0 讨论(0)
提交回复
热议问题