I am trying to change sign of columns(X,V) based on sign of C for multi-level dataframe with 0-62 columns. With condition do nothing if V is already -ve.
Dataframe sample
0 1
A X V C X V C
0 foo 1.2 1 -2 1.3 2 4
1 bar 4.1 4 6 1.2 -5 -1
What I am trying to achieve is change sign of C and D for all columns if E is -ve
0 1
A X V C X V C
0 foo -1.2 -1 -2 1.3 2 4
1 bar 4.1 4 6 -1.2 -5 -1
I know how to change sign for one column for simple df but can't do it for multi-level df with all columns.
df['V'] = df['V'].apply(lambda df['V']: if df['C'] >= 0 else df['C']*-1)
I have the code as below for V only but it gives the error,
df['V'] = (
df.loc[:,(slice(None),'V')]
.apply(lambda df.loc[:,(slice(None),'V')].iloc[:, 2:62]:
if df.loc[:,(slice(None),'C')].iloc[:, 2:62][out.loc[:,(slice(None),'C')] >= 0]
else df.loc[:,(slice(None),'V')].iloc[:, 2:62]*-1)
)
.apply(lambda df.loc[:,(slice(None),'V')].iloc[:, 2:62]:
^
SyntaxError: invalid syntax