Skip to content Skip to sidebar Skip to footer

Pandas Multiindex Set Value Based On Boolean Mask

The objective is to assign value based on mask in a multiindex columns df as below. However, the code does not produced what Im expecting it to be.May I know which part of the cod

Solution 1:

let's replace them:

appenddf=pd.DataFrame(mask.replace({True:'N',False:'P'}).values,
              index=df.index,
              columns=pd.MultiIndex.from_product([df.columns.levels[0],['iii']]))

OR

appenddf=mask.replace({True:'N',False:'P'}).rename(columns={'A':'iii'},level=1)

output of appenddf:

    One     Two
    iii     iii
0PP1   N       N
2   N       N
3PP4   N       P

Post a Comment for "Pandas Multiindex Set Value Based On Boolean Mask"