0

How do I add 10 to any value in column col2 if it's value is 0. The general case for a larger data frame.

     col1 col2
row1    1   2
row2    3   0

So I get:

     col1 col2
row1    1   2
row2    3   10

This gets me the correct row:

df2.loc[lambda df2: df2['col2'] == 0 ] 

This does the entire row, which I don't want:

df2.loc[df2['col2'] == 0 ] = 10
mountainclimber11
  • 1,339
  • 1
  • 28
  • 51

1 Answers1

1

Use np.where()

import numpy as np

df['col2'] = np.where(df['col2']==0, 10, df['col2'])
Sociopath
  • 13,068
  • 19
  • 47
  • 75