跟进:这个问题
我有一些看起来像这样的数据:
Student Class Course Date Instructor
Alex Intro to Philosophy 11/4/20 Jake
James Algorithms 11/5/20 Ashley/Jake
Mike Spanish I 11/7/20 Ashley
Steven Vector Calculus 11/5/20 Jake
Denise Intro to Philosophy 11/8/20 Jake
Carol Intro to Philosophy 11/8/20 Jake
我怎样才能找到一个如下所示的计数或枢轴,给定日期的班级一次对一名教练计数。
我想要这样的事情:
Jake Ashley
Intro to Philosophy 2 0
Algorithms 1 1
Spanish I 0 1
Vector Calculus 1 0
Total 4 2
由于“讲师”是数据透视表的目标列(我错了,尽管我是“学生”,我这样做了),所以应该拆分并分解数据,以便按照原始答案中的说明应用数据透视表。
自己的用户对评论提供的修改:
df.Instructor = df.Instructor.str.split('/')
df = df.explode('Instructor')
然后:
df.pivot_table(index='Class', columns='Instructor', values='Student', aggfunc=pd.Series.count).fillna(0).astype(int)
其中“学生”可以是数据框的任何其他列。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句