title: How to subtract from column in Dataframe based on conditions from other columns in row
body: <p>I have a dictionary set up like the example below and I am trying to subtract values from a column in a DataFrame if the key in the row matches a value in the dictionary and the string column in that row is yes.</p>
  
  <pre><code>dic = {1:2300,
         2:3000,
         56:572
         37:2930}
  </code></pre>
  
  <pre><code>df = key   string   value
       1     yes      5000
       4     yes      2000
       56    no       1000
       56    yes      6000
       14    yes      3000
  </code></pre>
  
  <p>I would want the function to subtract the dictionary value from the value column ONLY if the key exists in the dictionary and the string==yes. The resulting Dataframe would look like the following:</p>
  
  <pre><code>after = key   string   value
          1     yes      2700
          4     yes      2000
          56    no       1000
          56    yes      5428
          14    yes      3000
  </code></pre>
  
  <p>I tried writing a loop going through each row of the Dataframe and subtracting the df['value'] by dic[row] if the key and string value matched, but it wasn't working after trying to tweak multiple things. </p>
  
  <pre><code>for row in df:
      if dic.get(row['key'],0)!=0 and row['string']=='yes':
          row['value']=row['value']-dic[row['key']]
  </code></pre>
  
  <p>This code didn't work and just returned "TypeError: string indices must be integers". What should I change to correct it?</p>