dataframe怎么索引多个列
时间: 2023-05-26 19:03:57 浏览: 66
可以使用方括号[],将多个列的列名组成一个列表输入即可,例如:
```python
import pandas as pd
df = pd.DataFrame({'name': ['Tom', 'Jerry', 'Mike'],
'age': [20, 25, 22],
'gender': ['M', 'M', 'F']})
# 索引单个列
print(df['name'])
# 0 Tom
# 1 Jerry
# 2 Mike
# Name: name, dtype: object
# 索引多个列
print(df[['name', 'age']])
# name age
# 0 Tom 20
# 1 Jerry 25
# 2 Mike 22
```
注意,多个列的列名需要放在列表中,并且两个方括号中间需要有一个逗号。如果没有逗号,则会被解析为多次对单个列的索引操作,效果不同。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)