我正在尝试使用CSV文件中的列值创建一个饼图,但是我似乎并没有采取正确的方法。我正在使用CSV文件(student_grades.csv),并且要使用csv文件中“结果”列中的数据创建饼图。该列中带有“ 1s”的行表示“通过”,而该列中带有“ 0s”的行表示“失败”。我正在尝试为成功和失败结果创建饼图。这是我到目前为止所做的,非常感谢和建议。
import matplotlib.pyplot as plt
import pandas as pd
doc = pd.read_csv('student_grades.csv')
pass_fail_result = doc['Result']
pass_result = []
fail_result = []
plt.pie(str(pass_fail_result))
plt.show()```
This is the csv file that I am using.
```Name,Gender,Attendance,Department,Result
Gabby Smith,Female,Yes,Engineering,1
Dan Jones,Male,Yes,Engineering,1
Benard Clark,Male,Yes,Engineering,0
Lizzy Ringgs,Female,Yes,Engineering,1
Derick H. Leo,Male,Yes,Engineering,0
Matt Dickson,Male,Yes,Engineering,1
Laura Wlliamson,Female,Yes,Engineering,1
Frank A. Mitt,Male,Yes,Engineering,0
Ahmed Hussaini,Male,Yes,Engineering,1
Claire Greg,Female,Yes,Engineering,1
Flora Richards,Female,Yes,Engineering,0
Andy Roy,Male,Yes,Engineering,0
Richard Jones,Male,Yes,Engineering,1
Harry Ben,Male,Yes,Engineering,1
Claudia Green,Female,Yes,Engineering,0
Hamza Yusuf,Male,Yes,Engineering,1
Ella Leonard,Female,Yes,Engineering,0
Patrick Silva,Male,Yes,Engineering,1
答案 0 :(得分:1)
这将在饼图中绘制1的数字和0的数字:
plt.pie(doc['Result'].value_counts())
获得%:
plt.pie(100*df['Result'].value_counts()/len(df))
答案 1 :(得分:-1)
import sys, csv
def doSomething(filePath):
try:
with open(filePath, mode='r') as csvfile:
csvreader = csv.reader(csvfile, delimiter=',')
for row in csvreader:
if (row[2] is "SOMETHING"): # row[2] it's the cvs position
else: # DO SOMETHING ELSE
except FileNotFoundError:
print("[ERROR] Cannot open the file: ", filePath)