I have the empty data frame, with the header of the Data frame indicating the groups. Example,
df = pd.DataFrame(columns=['group1', 'group2', 'group3', 'group4', 'group5', 'group6'])
Now, I want add the sub-headers to each empty column in loop, because the original data frame is long. I have tried the code given below,
for ii in range(len(df.columns)):
df.columns[ii] = pd.MultiIndex.from_product([[ii],['condition_1','condition_2']])
The error I am getting is 'Index does not support mutable operations'. The expected output is,
| group1 | group2 | group3 | group4 | group5 | group6 | ||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| condition_1 | condition_2 | condition_1 | condition_2 | condition_1 | condition_2 | condition_1 | condition_2 | condition_1 | condition_2 | condition_1 | condition_2 |
How can I add the sub-headers (condition_1 and condition_2) to the to the pandas Data frame in the loop in python?
Thank you.
condition1andcondition2for everygroup1togroup6right? you double the columns from 6 to 12 columns with that IIUC.output. I think it is easier for everyone to figure outpd.DataFrame(columns=pd.MultiIndex.from_product([df.columns, ['condition_1','condition_2']]))?