Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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
Very short with loc:
loc
df.loc[df['column1']=='a', 'column2'] *= 10
You get:
column1 column2 0 a 320 1 b 175 2 b 165 3 a 800
Add a comment
Try np.where
np.where
df['column2'] = np.where(df['column1'].eq('a'), df['column2'].mul(10), df['column2'])
Or df.mask
df.mask
df['column2'] = df['column2'].mask(df['column1'].eq('a'), df['column2'].mul(10))
Required, but never shown
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.
Explore related questions
See similar questions with these tags.