I can not figure out how to simply/efficiently add a column to a dataframe that has a multiindex by assignment from another single level index
I can add a column to a dataframe that has a single level index as follows:
df = pd.DataFrame({'data':[1, 2, 5]},
index=pd.Index(['Alpha', 'Bravo', 'Echo'], name='item1_name'))
df
df_item1 = pd.DataFrame({'id':[101, 102, 103, 104, 105]},
index=pd.Index(['Alpha', 'Bravo', 'Charlie', 'Delta', 'Echo'], name='item1_name'))
df_item1
df['item1_id']=df_item1['id']
df
What I cant figure out is how to do this on a single level of a multiindex dataframe. e.g.
df_multi = pd.DataFrame({'data':[1, 2, 5, 11, 12, 15]},
index=pd.Index([('Alpha', 'X'), ('Alpha', 'Y'), ('Bravo', 'X'),
('Bravo', 'Y'), ('Echo', 'X'), ('Echo', 'Y')],
name=('item1_name','item2_name')))
df_multi['item1_id']=df_item1['id']
df_multi
I just get NaNs as the indexes arent aligning. My big picture problem is that I am receiving data with a string name and i need to be able to replace with an integer id for both levels, item1_name and item2_name
I have long solutions using unstack/stack/reindex etc but it all seems a very long way around and I feel i ought to be able to join on the index If my second look up frame is this:
df_item2 = pd.DataFrame({'id':[201, 202]},
index=pd.Index(['X', 'Y'], name='item2_name'))
df_item2
what I want to end up with is





