Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running an operation on one column based on content from another column in Pandas Dataframe

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')
like image 574
obabs Avatar asked Dec 18 '22 09:12

obabs


2 Answers

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
like image 63
miradulo Avatar answered May 03 '23 15:05

miradulo


You should try this:

df.loc[ ( df.side == 'B' ), 'nominal' ] *= -1
like image 33
dashboard_m Avatar answered May 03 '23 13:05

dashboard_m