This is basically the dataframe:
col1 col2 col3 label
row1 1 0 1 1
row2 0 0 0 1
row3 1 1 1 0
row4 1 2 1 0
I basically need it to go over each row, and if label = 0, multiply all the values in the row by -1.
I've tried many different approaches, including:
df.ix[3] = df.ix[3].multiply(-1)
Which returns:
SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead
I've also tried dropping the row and replacing, which doesn't work because the indexes change.
In [156]: df.loc[df.label==0, df.columns.drop('label')] = \
df.loc[df.label==0, df.columns.drop('label')].mul(-1)
In [157]: df
Out[157]:
col1 col2 col3 label
row1 1 0 1 1
row2 0 0 0 1
row3 -1 -1 -1 0
row4 -1 -2 -1 0
or bit shorter version:
In [160]: df.loc[df.label==0, df.columns.drop('label')] *= -1
In [161]: df
Out[161]:
col1 col2 col3 label
row1 1 0 1 1
row2 0 0 0 1
row3 -1 -1 -1 0
row4 -1 -2 -1 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With