pandas 数据透视中自动合并列级别的问题

Problem with automatic merge column levels in pandas pivot

这是我的基础数据框:

Course      Subject      Score Teacher     Student 
course_1    subject_1    18    teacher1    student_1
course_1    subject_1    16    teacher1    student_3
course_1    subject_1    18    teacher1    student_2
course_1    subject_2    19    teacher1    student_1
course_1    subject_2    20    teacher1    student_3
course_1    subject_3    17    teacher1    student_2
course_1    subject_3    18    teacher1    student_1
course_1    subject_3    13    teacher1    student_3

我用这个方法创建了 pivot table:

pivot_df = pd.pivot_table(df, values="Score", index=["Course", "Subject"], columns=["Teacher", "Student"])

我想要这样的东西:

                        teacher1
                        student_1  student_2   student_3
course_1  subject_1       18           18           16
          subject_2       19           Nan          20
          subject_3       18           17           13
                    

但是 pandas 给我这个:

                        teacher1\student_1  teacher1\student_2   teacher1\student_3
course_1\subject_1                  18           18                    16
course_1\subject_2                  19           Nan                   20
course_1\subject_3                  18           17                    13

如何解决这个问题?

我没有重现您的问题(使用 pandas 版本 1.3.5 和 1.4.1 进行测试)。

我得到了预期的 MultiIndex:

Teacher             teacher1                    
Student            student_1 student_2 student_3
Course   Subject                                
course_1 subject_1      18.0      18.0      16.0
         subject_2      19.0       NaN      20.0
         subject_3      18.0      17.0      13.0

如图:

使用的输入:

df = pd.DataFrame({'Course': ['course_1', 'course_1', 'course_1', 'course_1', 'course_1', 'course_1', 'course_1', 'course_1'],
                   'Subject': ['subject_1', 'subject_1', 'subject_1', 'subject_2', 'subject_2', 'subject_3', 'subject_3', 'subject_3'],
                   'Score': [18, 16, 18, 19, 20, 17, 18, 13],
                   'Teacher': ['teacher1', 'teacher1', 'teacher1', 'teacher1', 'teacher1', 'teacher1', 'teacher1', 'teacher1'],
                   'Student': ['student_1', 'student_3', 'student_2', 'student_1', 'student_3', 'student_2', 'student_1', 'student_3']})