I'm trying to convert the negative value of rows in column 'nominal'
where the corresponding value in column 'side'
is equal to 'B'. I don't want to lose any rows that are not converted. I've tried this below but getting raise KeyError('%s not in index' % objarr[mask])
df[-df['nominal']].where(df['side']=='B')
Just use both conditions in a boolean index with &
.
df[(df.side == 'B') & (df.nominal < 0)]
or if you intend on modifying,
df.loc[(df.side == 'B') & (df.nominal < 0), 'nominal']
Example
>>> df = pd.DataFrame(dict(side=['A']*3+['B']*3, nominal = [1, -2, -2, 2, 6, -5]))
>>> df
nominal side
0 1 A
1 -2 A
2 -2 A
3 2 B
4 6 B
5 -5 B
>>> df.loc[(df.side == 'B') & (df.nominal < 0), 'nominal'] = 1000
>>> df
nominal side
0 1 A
1 -2 A
2 -2 A
3 2 B
4 6 B
5 1000 B
This is a very standard way for filtering data in Pandas that you'll come across often. See Boolean Indexing in the Pandas docs.
Update
For your updated problem description, we can just use the augmented assignment operator *=
to multiply our desired values by -1.
df.loc[(df.side == 'B'), 'nominal'] *= -1
Example
>>> df = pd.DataFrame(dict(nominal = [1, 2, 5, 3, 5, 3], side=['A']*3 + ['B']*3))
>>> df
nominal side
0 1 A
1 2 A
2 5 A
3 3 B
4 5 B
5 3 B
>>> df.loc[(df.side == 'B'), 'nominal'] *= -1
df
nominal side
0 1 A
1 2 A
2 5 A
3 -3 B
4 -5 B
5 -3 B
You should try this:
df.loc[ ( df.side == 'B' ), 'nominal' ] *= -1
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