pandas读取excel表格,A列是学生名字,B列是班级名称,对该表格数据透视统计,统计每个班级的学生名字
时间: 2024-12-02 17:13:57 浏览: 97
要使用Pandas来读取Excel表格并统计每个班级的学生名字,可以按照以下步骤操作:
1. **导入所需的库**[^2]:
```python
import pandas as pd
```
2. **读取Excel文件**,假设文件名为`students.xlsx`,并且A列为姓名(Name),B列为班级(Class):
```python
df = pd.read_excel('students.xlsx', usecols=['Name', 'Class'])
```
3. **创建透视表以按班级分组统计学生数量**[^1]:
```python
pivot_table = pd.pivot_table(df, values='Name', index='Class', aggfunc='count')
```
这里`values='Name'`表示我们要对名字列计数,`index='Class'`指定班级作为行索引。
4. **查看结果**:
```python
print(pivot_table)
```
这将显示每个班级及其对应的student name的数量。
阅读全文
相关推荐












