dataframe.iloc
时间: 2023-10-30 12:18:59 浏览: 33
The iloc method in pandas is used to access the rows and columns of a dataframe using integer-based indexing.
Syntax: dataframe.iloc[row_index, column_index]
Example:
Consider the following dataframe:
```
import pandas as pd
data = {'name': ['John', 'Emily', 'Peter', 'Mary'],
'age': [25, 30, 35, 40],
'gender': ['M', 'F', 'M', 'F']}
df = pd.DataFrame(data)
```
Output:
```
name age gender
0 John 25 M
1 Emily 30 F
2 Peter 35 M
3 Mary 40 F
```
To access the first row of the dataframe, we can use iloc as follows:
```
df.iloc[0]
```
Output:
```
name John
age 25
gender M
Name: 0, dtype: object
```
To access the second column of the dataframe, we can use iloc as follows:
```
df.iloc[:, 1]
```
Output:
```
0 25
1 30
2 35
3 40
Name: age, dtype: int64
```
To access the value at the intersection of the second row and third column, we can use iloc as follows:
```
df.iloc[1, 2]
```
Output:
```
'F'
```