python iloc
时间: 2023-10-12 19:15:58 浏览: 32
iloc stands for "integer location" and is used in pandas to select rows and columns by their integer positions.
The iloc indexer is used to select rows and columns by their integer positions. It takes two arguments, the first argument specifies the row positions, and the second argument specifies the column positions.
Syntax:
```python
data.iloc[row_pos, col_pos]
```
Examples:
1. Select the first row and third column from a DataFrame:
```python
import pandas as pd
data = pd.read_csv('data.csv')
selected = data.iloc[0, 2]
```
2. Select the first three rows and first two columns from a DataFrame:
```python
import pandas as pd
data = pd.read_csv('data.csv')
selected = data.iloc[0:3, 0:2]
```
3. Select every third row and every second column from a DataFrame:
```python
import pandas as pd
data = pd.read_csv('data.csv')
selected = data.iloc[::3, ::2]
```
Note that iloc is used to select rows and columns based on their integer positions, while loc is used to select rows and columns based on their labels.