1.2 Dataframes and the ‘dot’ notation
In Week 2 you learned that dataframes have methods, which are like functions, that can only be called in the context of a dataframe.
For example, because the TB deaths dataframe df has a column named ‘Country’, the sort_values() method can be called like this:
In []:
df.sort_values('Country')
Because there is variable name, followed by a dot, followed by the method, this is called dot notation. Methods are said to be a property of a dataframe. In addition to methods, dataframes have another property – attributes.
Attributes
A dataframe attribute is like a variable that can only be accessed in the context of a dataframe. One such attribute is columns which holds a dataframe’s column names.
So the expression df.columns evaluates to the value of the columns attribute inside the dataframe df. The following code will get and display the names of the columns in the dataframe df:
In []:
df.columns
Out[]:
Index(['Country', 'Population (1000s)', 'TB deaths'],
dtype='object')