Sunil
Sunil

Reputation: 111

How to create multi index dataframe in pandas?

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

Upvotes: 1

Views: 68

Answers (1)

Valdi_Bo
Valdi_Bo

Reputation: 30971

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)

Upvotes: 1

Related Questions