Pandas: Assign MultiIndex Column from DataFrame

Pandas: Assign MultiIndex Column from DataFrame



I have a DataFrame with multiIndex columns. Suppose it is this:


index = pd.MultiIndex.from_tuples([('one', 'a'), ('one', 'b'),
('two', 'a'), ('two', 'b')])
df = pd.DataFrame('col': np.arange(1.0, 5.0), index=index)

df = df.unstack(1)



(I know this definition could be more direct). I now want to set a new level 0 column based on a DataFrame. For example


df['col2'] = df['col'].applymap(lambda x: int(x < 3))



This does not work. The only method I have found so far is to add each column seperately:
Pandas: add a column to a multiindex column dataframe
, or some sort of convoluted joining process.



The desired result is a new level 0 column 'col2' with two level 1 subcolumns: 'a' and 'b'


'col2'


'a'


'b'



Any help would be much appreciated, Thank you.




1 Answer
1



I believe need solution with no unstack and stack - filter by boolean indexing, rename values for avoid duplicates and last use DataFrame.append:


unstack


stack


boolean indexing


rename


DataFrame.append


df2 = df[df['col'] < 3].rename('one':'one1', 'two':'two1', level=0)
print (df2)
col
one1 a 1.0
b 2.0

df = df.append(df2)
print (df)
col
one a 1.0
b 2.0
two a 3.0
b 4.0
one1 a 1.0
b 2.0





Thank you for your answer, in reality I have a very large data frame with a multiindex on the columns, I want to avoid constantly stacking and unstacking as it will be computationally heavy for what is just an asignment. The unstack in my original post was just for the purposes of defining the data frame in the example (I should probably change that)
– Cameron Oliver
Sep 3 at 12:35





@CameronOliver - If my answer was helpful, don't forget accept it - click on the check mark beside the answer to toggle it from greyed out to filled in. Thanks.
– jezrael
Sep 4 at 6:31



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)