I need to generate a pd.DataFrame with columns being composed by a list and a Multiindex object, and I need to do it before filling the final dataframe with data.
Say the columns are ['one', 'two'] and the multiindex obtained from from_product:
import pandas as pd
col_21 = ['day', 'month']
col_22 = ['a', 'b']
mult_2 = pd.MultiIndex.from_product([ col_21, col_22 ])
I would like to get a list of columns which looks like this:
'one' | 'two' | ('day','a') | ('day','b') | ('month','a') | ('month','b')
One possible solution would be to use two different and separate Multiindex, one with a dummy column, both generate by from_product
col_11 = ['one', 'two']
col_12 = ['']
col_21 = ['day', 'month']
col_22 = ['a', 'b']
mult_1 = pd.MultiIndex.from_product([ col_11, col_12 ])
mult_2 = pd.MultiIndex.from_product([ col_21, col_22 ])
How could I get to this?
(one, '') | (two, '') | ('day','a') | ('day','b') | ('month','a') | ('month','b')
I have tried several trivial solutions, but each gave me a different error or a wrong result
mult_1+mult_2 #TypeError: cannot perform __add__ with this index type: MultiIndex
pd.merge #TypeError: Can only merge Series or DataFrame objects, a <class 'list'> was passed
pd.MultiIndex.from_arrays([ mult_1, mult_2 ]) #NotImplementedError: isna is not defined for MultiIndex
Thank you for your advices