python .iloc[]
时间: 2023-10-14 13:04:47 浏览: 37
The .iloc[] method in Python is used to select rows and columns of a DataFrame by their integer position. It is similar to the .loc[] method, but instead of using labels to select rows and columns, it uses integers.
The syntax for .iloc[] is as follows:
```
DataFrame.iloc[row_indexer, column_indexer]
```
where:
- `row_indexer`: an integer, a list of integers, or a slice object that selects the rows to be displayed.
- `column_indexer`: an integer, a list of integers, or a slice object that selects the columns to be displayed.
For example, to select the first two rows and the first three columns of a DataFrame, you can use the following code:
```
import pandas as pd
df = pd.read_csv('my_data.csv')
subset = df.iloc[0:2, 0:3]
```
This will create a new DataFrame called `subset` that contains the first two rows and the first three columns of `df`.