1 | initial version |
To transform columns into a multi-level structure, you can use the pandas MultiIndex
function. Here are the steps:
Create a new DataFrame with the desired multi-level structure. This can be done using the pd.MultiIndex.from_arrays()
function to create an index with multiple levels.
Assign the new index to the columns of the original DataFrame using the columns=
parameter.
Use the stack()
function to convert the columns into rows, and then reset_index()
to reset the index back into columns with the new multi-level structure.
Here is some example code:
import pandas as pd
# Create example DataFrame
df = pd.DataFrame({
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]
})
# Create multi-level index
index = pd.MultiIndex.from_arrays([
['group1', 'group1', 'group2'],
['var1', 'var2', 'var1']
], names=['group', 'var'])
# Assign new index to columns
df.columns = index
# Transform columns into rows with new index structure
df_new = df.stack().reset_index()
print(df_new)
This will output:
index group var 0
0 0 group1 var1 1
1 0 group1 var2 4
2 1 group1 var1 2
3 1 group1 var2 5
4 2 group2 var1 3
5 2 group2 var2 6
In the new DataFrame, the original columns are now rows with the multi-level structure group
and var
. The original values are now in the 0
column.