Contenido del Curso
Advanced Techniques in pandas
1. Get Familiar With Indexing and Selecting Data
Advanced Techniques in pandas
Learn More About Indexation
Let's move further and continue extracting columns and rows by indices. So, you need to be familiar with a function similar to loc[]
.
Our following function is iloc[]
; it is like index-location, and you may have guessed that it allows us to work with both columns' and rows' indices.
Firstly, we need to recall indices. The first row has the index 0
, the next one 1
, the next 2
, and so on. But also, we can count from the end (indeed, this is not convenient in datasets, but it may be helpful somehow), so the last one has the index -1
, the second-to-last is -2
, and so on...
Look at the table:
Index from the Beginning | Rows in Dataset | Index from the End |
---|---|---|
0 | Banana | -8 |
1 | Apple | -7 |
2 | Cucumber | -6 |
3 | Strawberry | -5 |
4 | Tomato | -4 |
5 | Orange | -3 |
6 | Lemon | -2 |
7 | Blueberry | -1 |
However, we will start with the simplest implementation of the function iloc[]
, working with the following data set (below are its first five rows):
Name | Age | City | |
---|---|---|---|
0 | John | 25 | New York |
1 | Emily | 30 | London |
2 | Michael | 35 | Paris |
3 | Sophia | 28 | Sydney |
4 | David | 40 | Tokyo |
Look to the code example and output:
data.iloc[0]
- extracts the very first row of the dataset;data.iloc[1]
- extracts the second row of the dataset;data.iloc[-1]
- extracts the very last row of the dataset;data.iloc[-2]
- extracts the second-to-last row of the dataset.
As you may have recognized, at the end of the output, the variable Name
shows the row number too, like Name: 998
.
Question
Replace the placeholders ___
in a code window with your code to answer the question below it.
Note that the index of a first person is 0.
¿Todo estuvo claro?
Contenido del Curso
Advanced Techniques in pandas
1. Get Familiar With Indexing and Selecting Data
Advanced Techniques in pandas
Learn More About Indexation
Let's move further and continue extracting columns and rows by indices. So, you need to be familiar with a function similar to loc[]
.
Our following function is iloc[]
; it is like index-location, and you may have guessed that it allows us to work with both columns' and rows' indices.
Firstly, we need to recall indices. The first row has the index 0
, the next one 1
, the next 2
, and so on. But also, we can count from the end (indeed, this is not convenient in datasets, but it may be helpful somehow), so the last one has the index -1
, the second-to-last is -2
, and so on...
Look at the table:
Index from the Beginning | Rows in Dataset | Index from the End |
---|---|---|
0 | Banana | -8 |
1 | Apple | -7 |
2 | Cucumber | -6 |
3 | Strawberry | -5 |
4 | Tomato | -4 |
5 | Orange | -3 |
6 | Lemon | -2 |
7 | Blueberry | -1 |
However, we will start with the simplest implementation of the function iloc[]
, working with the following data set (below are its first five rows):
Name | Age | City | |
---|---|---|---|
0 | John | 25 | New York |
1 | Emily | 30 | London |
2 | Michael | 35 | Paris |
3 | Sophia | 28 | Sydney |
4 | David | 40 | Tokyo |
Look to the code example and output:
data.iloc[0]
- extracts the very first row of the dataset;data.iloc[1]
- extracts the second row of the dataset;data.iloc[-1]
- extracts the very last row of the dataset;data.iloc[-2]
- extracts the second-to-last row of the dataset.
As you may have recognized, at the end of the output, the variable Name
shows the row number too, like Name: 998
.
Question
Replace the placeholders ___
in a code window with your code to answer the question below it.
Note that the index of a first person is 0.
¿Todo estuvo claro?