0

It seems that extracting values from a Pandas dataframe using attributes is inconsistent when I have a column name 'name'. When I do df.name I am expecting the value instead I am getting the index. Is this a quirk or should I not try to access the values by attributes?

See example:

>>>my_df = pd.DataFrame({'age':[10, 30, 40], 'name':['John', 'Bob', 'Alice'], 'city': ['Bangkok', 'Amsterdam', 'New York']})
>>>my_df
   age   name       city
0   10   John    Bangkok
1   30    Bob  Amsterdam
2   40  Alice   New York
>>>index, val = next(my_df.iterrows())
>>>index
0
>>>val.age
10
>>>val.city
'Bangkok'
>>>val.name
0    # <- had expected 'John' !
>>>val['name']
'John'
Bruno Vermeulen
  • 2,970
  • 2
  • 15
  • 29
  • name is a pandas function , when you do . chain it will default return the function not the column – BENY Aug 16 '20 at 02:52
  • so that means although it may work for many column names, I should not use the get by attribute method to get the values but to use `df['column name']` ? – Bruno Vermeulen Aug 16 '20 at 02:56
  • 2
    Use ['name'] is more safe ~ – BENY Aug 16 '20 at 02:57
  • 1
    It's issue #4 in YaOzl's solution: https://stackoverflow.com/questions/41130255/for-pandas-dataframe-whats-the-difference-between-using-squared-brackets-or-do – ALollz Aug 16 '20 at 02:58
  • Also there's a slight subtlety here. There is no `name` attribute for DataFrames. But since you use `iterrrows` that passes a [`Series`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.html) which has a `name` attribute. – ALollz Aug 16 '20 at 03:06

0 Answers0