Data-frame Object has no Attribute
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.
Related videos on Youtube
Mouna Belaid
Updated on January 14, 2022Comments
-
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.
When I type
data.Country
anddata.Year
, I get the 1st Column and the second one displayed. However when I typedata.Number
, everytime it gives me this error:AttributeError: 'DataFrame' object has no attribute 'Number'.
-
Jeff almost 8 yearsIt's hard to say without being able to load your data. Did you try using
data['Number']
to access that column? Type indf.columns.values
to see an array of your column names.
-
-
Merlin almost 8 yearsLet me give you my favorite tutorial, enjoy! people.duke.edu/~ccc14/sta-663/UsingPandas.html
-
Shanteshwar Inde about 5 yearsHi, welcome to Stackoverflow!! It would be great if you could read these guidelines before answering any question. Thanks.
-
Rajan almost 4 yearsWhen 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 over 2 yearsWorked like a charm! Thanks a lot.