2

I would need to update some information on a column called Value:

Value Home
12     123
30     124
21     124
21     99

I would like to add to Value 0.5 if Home has value 124. I am thinking of using .loc to select rows but I would need to understand how to update the value. My expected results should be

Value Home
12     123
30.5   124
21.5   124
21     99

Any tip is appreciated.

still_learning
  • 776
  • 9
  • 32

2 Answers2

4
df.loc[df['Home']==124, 'Value'] += .5
Chris
  • 15,819
  • 3
  • 24
  • 37
  • 1
    thank you to both. May I ask you if this solution works also with another condition based on a different column (for example if Property is > 20)? – still_learning Oct 08 '20 at 01:26
  • 1
    @still_learning sure: `df.loc[(df['Home']==124) & (df['Property'] > 20), 'Value']+=5` – Chris Oct 08 '20 at 01:38
2

Use mask

mask = (df.Home == 124)
df.loc[mask,'Value'] = df.loc[mask,'Value'] + 0.5
Chris
  • 15,819
  • 3
  • 24
  • 37
Sivaram Rasathurai
  • 5,533
  • 3
  • 22
  • 45