The `iloc` Property
The iloc
property in Pandas allows us to access specific parts of a DataFrame using numerical positions.
You can use iloc
to access:
- A single element;
- A single row;
- A single column;
- A range of rows;
- A range of columns;
- A range of rows and columns.
General Syntax
DataFrame.iloc[row_position, column_position]
You can also slice:
DataFrame.iloc[start_row:end_row, start_col:end_col]
Example
12345678910111213141516171819202122232425import pandas as pd df = pd.DataFrame({ 'Name': ['Alice', 'Bob', 'Charlie', 'Daisy'], 'Age': [25, 30, 35, 28], 'City': ['New York', 'Paris', 'London', 'Berlin'] }) # Single element print('>> Single Element:\n', df.iloc[1, 2]) # Single row print('\n>> Single Row:\n', df.iloc[2]) # Single column print('\n>> Single Column:\n', df.iloc[:, 1]) # Range of rows print('\n>> Range of Rows:\n', df.iloc[1:3]) # Range of columns print('\n>> Range of Columns:\n', df.iloc[:, 0:2]) # Range of rows and columns print('\n>> Range of Rows & Columns:\n', df.iloc[1:3, 0:2])
Tout était clair ?
Merci pour vos commentaires !
Section 3. Chapitre 10
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Suggested prompts:
Posez-moi des questions sur ce sujet
Résumer ce chapitre
Afficher des exemples du monde réel
Awesome!
Completion rate improved to 2.7
The `iloc` Property
Glissez pour afficher le menu
The iloc
property in Pandas allows us to access specific parts of a DataFrame using numerical positions.
You can use iloc
to access:
- A single element;
- A single row;
- A single column;
- A range of rows;
- A range of columns;
- A range of rows and columns.
General Syntax
DataFrame.iloc[row_position, column_position]
You can also slice:
DataFrame.iloc[start_row:end_row, start_col:end_col]
Example
12345678910111213141516171819202122232425import pandas as pd df = pd.DataFrame({ 'Name': ['Alice', 'Bob', 'Charlie', 'Daisy'], 'Age': [25, 30, 35, 28], 'City': ['New York', 'Paris', 'London', 'Berlin'] }) # Single element print('>> Single Element:\n', df.iloc[1, 2]) # Single row print('\n>> Single Row:\n', df.iloc[2]) # Single column print('\n>> Single Column:\n', df.iloc[:, 1]) # Range of rows print('\n>> Range of Rows:\n', df.iloc[1:3]) # Range of columns print('\n>> Range of Columns:\n', df.iloc[:, 0:2]) # Range of rows and columns print('\n>> Range of Rows & Columns:\n', df.iloc[1:3, 0:2])
Tout était clair ?
Merci pour vos commentaires !
Section 3. Chapitre 10