0

Say I have 2 dataframes and each dataframe has the same columns but hold different types of information.

Example:

animals_data = pd.DataFrame({'A':['Dog', 'Cat'], 'B': ['Horse', 'Donkey']})
numbers_data = pd.DataFrame({'A':[1, 2], 'B': [3, 4]})

My goal is to create a hierarchical (also known as MultiIndex) dataframe where the first level is Animals & Numbers, and then the second level is column A and column B.

That is,

MultiIndex_data['Animals']

A   |   B
-----------
Dog | Horse
Cat | Donkey

and

MultiIndex_data['Numbers']

A | B
-----
1 | 3
2 | 4

How can I go about doing this?

0

1 Answer 1

0

Do you mean:

pd.concat([animals_data, numbers_data], keys=['animal','number'], axis=1)

Output:

  animal         number   
       A       B      A  B
0    Dog   Horse      1  3
1    Cat  Donkey      2  4
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.