How to resolve AttributeError: 'DataFrame' object has no attribute
Question:
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
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’.
Answers:
I think the column name that contains "Number"
is something like " Number"
or "Number "
. I’m assuming you might have a residual space in the column name. Please run print "<{}>".format(data.columns[1])
and see what you get. If it’s something like < Number>
, it can be fixed with:
data.columns = data.columns.str.strip()
In general, AttributeError: 'DataFrame' object has no attribute '...'
, where ...
is some column name, is caused because .
notation has been used to reference a nonexistent column name or pandas method.
pandas methods are accessed with a .
. pandas columns can also be accessed with a .
(e.g. data.col
) or with brackets (e.g. ['col']
or [['col1', 'col2']]
).
data.columns = data.columns.str.strip()
is a fast way to quickly remove leading and trailing spaces from all column names. Otherwise verify the column or attribute is correctly spelled.
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'})
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.
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.
data = pd.read_csv('/your file name', delim_whitespace=True)
data.Number
now you can run this code with no error.
Change ";" for "," in the csv file
I realize this is not the same usecase but this might help:
In my case, my DataFrame object didn’t have the column I wanted to do an operation on.
The following conditional statement allowed me to avoid the AttributeError
:
if '<column_name>' in test_data.columns:
# do your operation on the column
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
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’.
I think the column name that contains "Number"
is something like " Number"
or "Number "
. I’m assuming you might have a residual space in the column name. Please run print "<{}>".format(data.columns[1])
and see what you get. If it’s something like < Number>
, it can be fixed with:
data.columns = data.columns.str.strip()
In general, AttributeError: 'DataFrame' object has no attribute '...'
, where ...
is some column name, is caused because .
notation has been used to reference a nonexistent column name or pandas method.
pandas methods are accessed with a .
. pandas columns can also be accessed with a .
(e.g. data.col
) or with brackets (e.g. ['col']
or [['col1', 'col2']]
).
data.columns = data.columns.str.strip()
is a fast way to quickly remove leading and trailing spaces from all column names. Otherwise verify the column or attribute is correctly spelled.
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'})
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.
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.
data = pd.read_csv('/your file name', delim_whitespace=True)
data.Number
now you can run this code with no error.
Change ";" for "," in the csv file
I realize this is not the same usecase but this might help:
In my case, my DataFrame object didn’t have the column I wanted to do an operation on.
The following conditional statement allowed me to avoid the AttributeError
:
if '<column_name>' in test_data.columns:
# do your operation on the column