1

I have a dataframe like:

 column1  column2
   a        32
   b       175
   b       165
   a        80

and I want to update the value (multiplied by 10) of every row of column2 whose value in column1 == 'a'. The result is expected to be like:

 column1  column2
   a       320
   b       175
   b       165
   a       800

2 Answers 2

2

Very short with loc:

df.loc[df['column1']=='a', 'column2'] *= 10

You get:

  column1  column2
0       a      320
1       b      175
2       b      165
3       a      800
Sign up to request clarification or add additional context in comments.

Comments

1

Try np.where

df['column2'] = np.where(df['column1'].eq('a'), df['column2'].mul(10), df['column2'])

Or df.mask

df['column2'] = df['column2'].mask(df['column1'].eq('a'), df['column2'].mul(10))
  column1  column2
0       a      320
1       b      175
2       b      165
3       a      800

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.