Accessing Data¶
Reminder: We currently have a dataframe called measurements
and it looks like this:
Sneezes Temperature Humidity
Monday 32 10.9 62.5
Tuesday 41 8.2 76.3
Wednesday 56 7.6 82.4
Thursday 62 7.8 98.2
Friday 30 9.4 77.4
Saturday 22 11.1 58.9
Sunday 17 12.4 41.2
Selecting Columns¶
To get all available column names, run
We can extract a singular column by using the []
-operator:
Output
Note that the output is a series again
To access a selection of columns, we pass in a list of column names in the desired order
Output
Selecting Rows¶
To access given rows you can use the slicing operation as known from lists:
If you pass in a singular number instead of [start:stop]
pandas will look for a row with that number as a label.
This will fail in our example since the rows are not numbered.
Acess via loc
¶
The property loc
gives label-based access to the elements of a dataframe.
It follows the pattern dataframe.loc[row_slice, column_slice]
.
For example:
Output
Access via iloc
¶
The iloc
-property works similar to loc
, except that it takes integer-based indexes instead of row/column labels:
Output same as above
Key Points
- Rows and columns can be selected ba their label, with the
loc
- oriloc
-methods