繁体   English   中英

Pandas:如何 plot 条形图每个月超过计数

[英]Pandas : How to plot bar graph for each month over counts

我有一个 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 刻度:2019 年 12 月、2020 年 1 月、2020 年 2 月)和y-axis - Student_Performance == 的学生总数(计数) Poor

请给任何想法,在此先感谢。

希望下面的代码片段是您正在寻找的:

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

output: 在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM