You can select the rows of a DataFrame one of two ways. One way is with df.iloc[...]. df.iloc[...] selects rows based on their position in the DataFrame. To select a single row, you can do df.iloc[position], for example, df.loc[1] selects the row with Esha below. To select multiple rows, you can do df.iloc[[position1, position2]], for example, df.loc[[0, 2]].
Suppose you constructed a DataFrame by
import pandas as pd df = pd.DataFrame({'name': ['Jeff', 'Esha', 'Jia'], 'age': [30, 56, 8]}, index=[132, 156, 27])
Where the index value is the person id in a database.
Giving you the DataFrame
| name | age | |
|---|---|---|
| 132 | Jeff | 30 |
| 156 | Esha | 56 |
| 27 | Jia | 8 |
Complete the function, select_first_row(df), by having it return the first row in the DataFrame.
df = pd.DataFrame({'name': ['Jeff', 'Esha', 'Jia'], 'age': [30, 56, 8]}, index=[132, 156, 27])
| name | age | |
|---|---|---|
| 132 | Jeff | 30 |
| 156 | Esha | 56 |
| 27 | Jia | 8 |
| 132 | |
|---|---|
| name | Jeff |
| age | 30 |