index = pd.MultiIndex.from_tuples(tuples, names=["first", "second"])
df.columns = index
print(df) [/code] [code]first level_0 second A B C D 0 foo small 1 2 1 foo large 2 4 2 bar large 2 5 3 bar small 7 9 [/code] Действие поворота выполняется над кадром данных: [code]table = pd.pivot_table( df, values=[('level_0', 'C')], index=[('level_0', 'A')], columns=[('level_0', 'B')] ) print(table) [/code] [code]first level_0 second C ('level_0', 'B') large small (level_0, A) bar 2.0 7.0 foo 2.0 1.0 [/code] Вывод содержит информацию о несуществующих столбцах: [code]print(table.columns.levels) # [['level_0'], ['A', 'B', 'C', 'D'], ['large', 'small']] [/code] Столбец [b]D[/b] не существует в кадре данных, но присутствует в выводе columns.levels.