1

How to create multi index data enter image description hereframes in pandas ?

1 Answer 1

1

First option

One of possibe solutions is to create the MultiIndex from tuples:

Start from defining source tuples:

tpl = [('', 'Material'), ('', 'Brand'),
    ('abcd.com', 'Stock'), ('abcd.com', 'Sales'), ('abcd.com', 'Leftover'),
    ('xyz.com',  'Stock'), ('xyz.com',  'Sales'), ('xyz.com', 'Leftover')]

Each tuple contains respective column name at consecutive levels.

Then create the MultiIndex:

cols = pd.MultiIndex.from_tuples(tpl)

And now you can create your DataFrame, calling e.g.:

df = pd.DataFrame(<your_data>, columns=cols)

Another option

The second choice is to create the MultiIndex from arrays:

Source data is actually a list containing column names (also lists) for each level:

arr = [[ '', '', 'abcd.com', 'abcd.com', 'abcd.com', 'xyz.com', 'xyz.com', 'xyz.com' ],
       [ 'Material', 'Brand', 'Stock', 'Sales', 'Leftover', 'Stock', 'Sales', 'Leftover']]

Then, to create the MultiIndex, call:

ind = pd.MultiIndex.from_arrays(arr)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.