Skip to content Skip to sidebar Skip to footer

Assign Value Based On Conditional Of Two Multiindex Columns In Pandas

The objective is to create a new multiindex column (stat) based on the condition of the column (A and B) Condition for A CONDITION_A='n'if A<0 else 'p' and Condition for B COND

Solution 1:

You can try concatinating both df's:

s=pd.concat([appenddf,appenddf_2],axis=1)
cols=pd.MultiIndex.from_product([s.columns.get_level_values(0),['stat']])
out=pd.concat([s.loc[:,(x,slice(None))].agg('_'.join,axis=1) for x in s.columns.get_level_values(0).unique()],axis=1,keys=cols)

output of out:

    One     Two
    statstat
0   P_g     P_l
1   N_l     N_l
2   N_l     N_g
3   P_g     P_l
4   N_l     P_l

Post a Comment for "Assign Value Based On Conditional Of Two Multiindex Columns In Pandas"