0
data = {'0':['Col1', 'Col1_1', 10,9,8],'1':['Col1', 'Col1_2', 10,9,8], '2':['Col2', 'Col1_1', 10,9,8],'2':['Col2', 'Col1_2', 10,9,8]} df1 = pd.DataFrame(data) 

I'd like to modify the code above to where the first 2 columns in the dataframe are multi-index columns? So moving "Col1" and "Col1_1" to be columns in a muli-index dataframe.

1 Answer 1

2

See if this is what you are asking for :

df1.columns=pd.MultiIndex.from_arrays(df1.iloc[0:2].values) #delete the first 2 rows because they are also the columns df1=df1.iloc[2:] df1.reset_index(drop=True) 

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Welcome to Stack Overflow! After you ask a question here, if you get an acceptable answer, you should “accept” the answer by clicking the check mark next to it. This scores points for you and for the person who answered your question. You can find out more about accepting answers here: How do I accept an answer?, Why should we accept answers?, What should I do if someone answers my question?.
Welcome to Stack Overflow! This is not a forum. If you wish to thank somebody, you should vote him up. We require a little bit of reputation before you can do that to prevent abuse; why don't you get some by answering a question?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.