pandas slice用法
时间: 2023-10-13 17:04:56 浏览: 139
Pandas提供了很多方法来对数据进行切片,其中使用loc进行标签(label)切片,iloc进行整数位置切片,ix混合切片。具体使用方法如下:
- loc: 通过标签进行切片
- 选取指定行、列:
```python
df.loc[row_indexer, column_indexer]
```
- 选取所有行、指定列:
```python
df.loc[:, column_indexer]
```
- 选取指定行、所有列:
```python
df.loc[row_indexer, :]
```
- 选取指定行、指定列:
```python
df.loc[row_indexer, column_indexer]
```
- iloc: 通过整数位置进行切片
- 选取指定行、列:
```python
df.iloc[row_indexer, column_indexer]
```
- 选取所有行、指定列:
```python
df.iloc[:, column_indexer]
```
- 选取指定行、所有列:
```python
df.iloc[row_indexer, :]
```
- 选取指定行、指定列:
```python
df.iloc[row_indexer, column_indexer]
```
- ix: 混合切片
- 选取指定行、列:
```python
df.ix[row_indexer, column_indexer]
```
- 选取所有行、指定列:
```python
df.ix[:, column_indexer]
```
- 选取指定行、所有列:
```python
df.ix[row_indexer, :]
```
- 选取指定行、指定列:
```python
df.ix[row_indexer, column_indexer]
```
其中,row_indexer和column_indexer可以是一个标签(loc)、整数位置(iloc)或标签和整数位置的混合(ix)。
阅读全文
相关推荐


















