如何根据出勤率绘制图表

时间:2019-02-01 10:43:33

标签: python pandas matplotlib

我有一个csv文件,其中包含一些学生在特定日期的出勤情况。

这是我的csv文件

Name,RollNumber,Attendance,Date,Day,Time
student1,1,Present,1/30/2019,Wednesday,12:34:05
student2,2,Present,1/30/2019,Wednesday,12:34:05
student3,3,Present,1/30/2019,Wednesday,12:34:05
student4,4,Present,1/30/2019,Wednesday,12:34:05
student1,1,Absent,1/31/2019,Thursday,23:34:05
student2,2,Present,1/31/2019,Thursday,23:34:05
student3,3,Present,1/31/2019,Thursday,23:34:05
student4,4,Present,1/31/2019,Thursday,12:34:05
student1,1,Present,2/1/2019,Friday,12:34:05
student2,2,Absent,2/1/2019,Friday,12:34:05
student3,3,Absent,2/1/2019,Friday,12:34:05
student4,4,Present,2/1/2019,Friday,12:34:05
student1,1,Absent,2/2/2019,Saturday,12:34:05
student2,2,Absent,2/2/2019,Saturday,12:34:05
student3,3,Absent,2/2/2019,Saturday,12:34:05
student4,4,Absent,2/2/2019,Saturday,12:34:05

我想绘制一个图表,显示csv文件中每个日期在场和不在场的学生人数。如何使用matplotlib做到这一点?

1 个答案:

答案 0 :(得分:1)

我认为最简单的方法是按照以下方式使用熊猫pivot_table

df = pd.read_csv('your_csv_filepath_here')

# Create a duplicate of your target value
df['attendance'] = a.Attendance

# Pivot your dataframe
df_pivot = df.pivot_table(index=['Date'], columns='Attendance', values='attendance', aggfunc='count')

# Plot it using pandas (barplot is probably what you want)
df_pivot.plot(kind='bar')

Result I got

当然,可以进一步自定义图,其他方法也可以达到相同的结果