laszlopanaflex
laszlopanaflex

Reputation: 1916

conditionally multiply values in DataFrame row

here is an example DataFrame:

df = pd.DataFrame([[1,0.5,-0.3],[0,-4,7],[1,0.12,-.06]], columns=['condition','value1','value2'])

I would like to apply a function which multiples the values ('value1' and 'value2' in each row by 100, if the value in the 'condition' column of that row is equal to 1, otherwise, it is left as is.

presumably some usage of .apply with a lambda function would work here but I am not able to get the syntax right. e.g.

df.apply(lambda x: 100*x if x['condition'] == 1, axis=1) 

will not work

the desired output after applying this operation would be:

enter image description here

Upvotes: 4

Views: 8012

Answers (4)

BENY
BENY

Reputation: 323276

As simple as

df.loc[df.condition==1,'value1':]*=100

Upvotes: 7

Vishnudev Krishnadas
Vishnudev Krishnadas

Reputation: 10960

Use a mask to filter and where it is true choose second argument where false choose third argument is how np.where works

value_cols = ['value1','value2']
mask = (df.condition == 1)
df[value_cols] = pd.np.where(mask[:, None], df[value_cols].mul(100), df[value_cols])

If you have multiple value columns such as value1, value2 ... and so on, Use

value_cols = df.filter(regex='value\d').columns    

Upvotes: 0

anky
anky

Reputation: 75080

Use df.loc[] with the condition and filter the list of cols to operate then multiply:

l=['value1','value2'] #list of cols to operate on
df.loc[df.condition.eq(1),l]=df.mul(100)
#if condition is just 0 and 1 -> df.loc[df.condition.astype(bool),l]=df.mul(100)
print(df)

Another solution using df.mask() using same list of cols as above:

df[l]=df[l].mask(df.condition.eq(1),df[l]*100)
print(df)

   condition  value1  value2
0          1    50.0   -30.0
1          0    -4.0     7.0
2          1    12.0    -6.0

Upvotes: 2

tawab_shakeel
tawab_shakeel

Reputation: 3739

import numpy as np

df['value1'] = np.where(df['condition']==1,df['value1']*100,df['value1']
df['value2'] = np.where(df['condition']==1,df['value2']*100,df['value2']

In case multiple columns

# create a list of columns you want to apply condition
columns_list = ['value1','value2']
for i in columns_list:
     df[i] = np.where(df['condition']==1,df[i]*100,df[i]

Upvotes: 4

Related Questions