Bracket and dot notation
In the previous section, we focused on the DataFrame.loc
method. pandas offers two ways to select data – using just brackets, []
, and using what is called dot notation (pandas also refers to the latter as attribute access since object.name
is Python syntax for accessing the name
attribute in object
).
Bracket notation
We have already introduced one form of bracket notation, which is using a column name inside brackets. There are several ways to apply bracket notation to a DataFrame, as follows:
- Select entire columns:
DataFrame['column_name']
orDataFrame[[list of column names]]
. If a single column is selected, the result is a Series; otherwise, the result is a DataFrame. If an additional selection results in only one row, the result can be a Series. Also, if the DataFrame only contains one row, selecting one column returns a Series (even though the result is a single value). - Selecting a range of rows:
DataFrame[start:end...