`iloc` 是 Pandas 库中用于基于整数位置选择数据的函数。以下是 `iloc` 的一些基本用法:
选择单个元素
```python
df.iloc[row_index, col_index]
选择多个元素
```python
df.iloc[start_row:end_row, start_col:end_col]
选择特定行
```python
df.iloc[row_indices]
选择特定列
```python
df.iloc[:, col_indices]
选择行和列的组合
```python
df.iloc[row_indices, col_indices]
使用布尔索引选择元素
```python
df.iloc[boolean_index]
请注意,`iloc` 函数中的索引是基于0的,即第一个元素的索引为0。
```python
import pandas as pd
创建一个 DataFrame
data = {
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]
}
df = pd.DataFrame(data)
选择特定行和列的元素
element = df.iloc[1, 2]
print(element) 输出结果为 8
选择多行和多列的元素
subset = df.iloc[0:2, 1:3]
print(subset) 输出结果为:
B C
0 4 7
1 5 8
希望这些示例能帮助你理解 `iloc` 的用法。