python一次选取多列
时间: 2023-07-05 10:36:32 浏览: 43
可以使用 Pandas 库中的 iloc 函数来选取多列。例如,假设有一个名为 df 的 DataFrame,其中包含 5 列,可以通过以下方式选取第 1、3 和 5 列:
```python
df.iloc[:, [0, 2, 4]]
```
其中,`:` 表示选取所有行,`[0, 2, 4]` 表示选取第 1、3 和 5 列。
相关问题
python dataframe选取多列
要选取Python DataFrame中的多列,可以使用以下方法:
1. 使用列名列表选取多列:
```python
df[['列名1', '列名2', '列名3']]
```
2. 使用iloc方法选取多列:
```python
df.iloc[:, [列索引1, 列索引2, 列索引3]]
```
3. 使用loc方法选取多列:
```python
df.loc[:, ['列名1', '列名2', '列名3']]
```
其中,df是DataFrame对象,列名和列索引分别表示要选取的列的名称和索引。
python iloc选取一个行的多列
你可以使用pandas中的iloc方法来选取一个行的多列。具体操作是,先选择行,再选择列,如下所示:
```
import pandas as pd
# 创建一个DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
# 选取第二行的第一列和第三列
result = df.iloc[1, [0, 2]]
print(result)
```
输出结果为:
```
A 2
C 8
Name: 1, dtype: int64
```