4

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

laszlopanaflex
  • 1,836
  • 3
  • 23
  • 34
  • 1
    as said, if the value in the 'condition' column equals 1, so in this case, the first and third row would have 'value1' and 'value2' multiplied by 100 – laszlopanaflex Jul 20 '19 at 16:06

4 Answers4

7

As simple as

df.loc[df.condition==1,'value1':]*=100
BENY
  • 317,841
  • 20
  • 164
  • 234
4
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]
tawab_shakeel
  • 3,701
  • 10
  • 26
2

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
anky
  • 74,114
  • 11
  • 41
  • 70
0

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    
Vishnudev Krishnadas
  • 10,679
  • 2
  • 23
  • 55