Reputation: 111
How to create multi index data frames in pandas ?
Upvotes: 1
Views: 68
Reputation: 30971
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)
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