后续链接:This question
我有一些数据,看起来像这样:
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发布于 2020-11-13 00:21:38
由于“讲师”是透视列的目标列(我错把它当成了“学生”),所以应该拆分和分解数据,以便应用原始答案中提供的pivot_table。
自己的用户在评论上提供的修改:
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)其中'Student‘可以是数据框中的任何其他列。
https://stackoverflow.com/questions/64807291
复制相似问题