pandas 如何绘制每个月超过计数的条形图

e3bfsja2  于 2023-09-29  发布在  其他
关注(0)|答案(1)|浏览(62)

我有一个dataframe df如下:

Student_id   Date_of_exam(d/m/y)  Student_Performance
1            1/4/2020              Excellent
1            30/12/2019            Poor
1            26/12/2019            Medium
2            3/1/2021              Medium
2            10/1/2021             Poor
3            4/5/2020              Poor
3            22/8/2020             Excellent

我怎样才能得到以x-axis为月-年的条形图(例如:y-ticks:2019年12月,2020年1月,2020年2月)和y-axis-其Student_Performance == Poor的学生总数(计数)。
请,做给予任何想法,提前感谢.

slsn1g29

slsn1g291#

希望下面的片段是你正在寻找的:

import seaborn as sns
import pandas as pd

df = pd.read_csv("student_data.csv") #assuming the data is in the csv file

# get only month and year from datetime column (date of exam)
df['date_col'] = df['Date_of_exam(d/m/y)'].dt.to_period('M') 

# group by based on month and year after filtering poor graded students
data = df[df['Student_Performance']=='Poor'].groupby(['date_col']).size().reset_index(name
    = 'count')  

ax = sns.barplot(x="date_col", y="count", data=data) #plot using seaborn

输出:

相关问题