panda读取csv文件每一行数据是一位同学的信息,并给每位同学绘制各科成绩饼图,
时间: 2023-12-14 08:36:51 浏览: 73
可以按照以下步骤实现:
1. 导入 pandas 和 matplotlib 库。
```python
import pandas as pd
import matplotlib.pyplot as plt
```
2. 使用 pandas 的 read_csv() 方法读取 csv 文件数据,并将数据保存为 DataFrame 对象。
```python
df = pd.read_csv('students.csv')
```
3. 遍历 DataFrame 对象中的每一行数据,使用 matplotlib 绘制各科成绩饼图。
```python
for index, row in df.iterrows():
# 获取每位同学的各科成绩
math_score = row['math']
english_score = row['english']
chinese_score = row['chinese']
# 绘制饼图
labels = ['Math', 'English', 'Chinese']
sizes = [math_score, english_score, chinese_score]
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
plt.axis('equal')
plt.title('Score of ' + row['name'])
plt.show()
```
完整代码如下:
```python
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('students.csv')
for index, row in df.iterrows():
math_score = row['math']
english_score = row['english']
chinese_score = row['chinese']
labels = ['Math', 'English', 'Chinese']
sizes = [math_score, english_score, chinese_score]
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
plt.axis('equal')
plt.title('Score of ' + row['name'])
plt.show()
```
其中,students.csv 文件的格式应该为:
```
name,math,english,chinese
张三,90,80,95
李四,85,75,80
王五,95,90,85
... ...
```
阅读全文