title: Sum values to column if another have a string Pandas
body: <p>I have a dataframe with values of different attributes, and i have a json file which have a list of attributes to sum only if a column in the dataframe contains a string.</p>
  <pre><code>| Product | store_location | attr1 | attr2 | attr3 | attr4 | global |
  | ------- | -------------- | ----- | ----- | ----- | ----- | ------ |
  | First   | NY-store1      | 3     | 5     |  2    | 2     |        |
  | Second  | NY-store2      | 1     | 3     |  5    | 1     |        |
  | Third   | NJ-store1      | 3     | 5     |  2    | 2     |        |
  | Fourth  | PA-store1      | 1     | 3     |  5    | 1     |        |
  </code></pre>
  <p>The json file has this structure:</p>
  <pre><code>{
  &quot;positionEvaluation&quot;: [
      {
        &quot;position&quot;: &quot;Global&quot;,
        &quot;sumElements&quot;: [&quot;attr1&quot;, &quot;attr2&quot;],
        &quot;gralSum&quot;: [&quot;attr2&quot;, &quot;attr3&quot;, &quot;attr4&quot;],
        &quot;elementsProm&quot;: [&quot;attr1&quot;, &quot;attr2&quot;, &quot;attr3&quot;, &quot;attr4&quot;]
      }
  ]
  }
  </code></pre>
  <p>Obviously the real file has more attributes, only for demo. So, I want when the product has in the store location the string 'NY' take respective attributes of &quot;sumElements&quot; and divide by the length of &quot;gralSum&quot;, and if the product has another string like 'NJ' or 'PA' just sum all elements of &quot;elementsProm&quot; and then divide by the length of it.</p>
  <p>Here my code:</p>
  <pre><code>for p in range(len(js_positions[&quot;positionEvaluation&quot;])):
      aux1_string = js_positions[&quot;positionEvaluation&quot;][p][&quot;position&quot;]
      df[aux1_string] = 0
      if df['store_location'].str.contains('NY').any():
          for k in range(len(js_positions[&quot;positionEvaluation&quot;][p][&quot;sumElements&quot;])):
              tmp = js_positions[&quot;positionEvaluation&quot;][p][&quot;sumElements&quot;][k]
              df[aux1_string] = df[aux1_string] + df[tmp_for_gk]
  
          df[aux1_string] = df[aux1_string] / len(js_positions[&quot;positionEvaluation&quot;][p][&quot;gralSum&quot;])
  
      else:
          for k in range(len(js_positions[&quot;positionEvaluation&quot;][p][&quot;elementsProm&quot;])):
              tmp = js_positions[&quot;positionEvaluation&quot;][p][&quot;elementsProm&quot;][k]
              df[aux1_string] = df[aux1_string] + df[tmp]
          df[aux1_string] = df[aux1_string] / len(js_positions[&quot;positionEvaluation&quot;][p][&quot;elementsProm&quot;])
  
  </code></pre>
  <p>Explicit list:</p>
  <pre><code>sumElements = [&quot;attr1&quot;, &quot;attr2&quot;]
  gralSum = [&quot;attr2&quot;, &quot;attr3&quot;, &quot;attr4&quot;]
  elementsProm = [&quot;attr1&quot;, &quot;attr2&quot;, &quot;attr3&quot;, &quot;attr4&quot;]
  </code></pre>
  <p>Expected output:</p>
  <pre><code>| Product | store_location | attr1 | attr2 | attr3 | attr4 | global |
  | ------- | -------------- | ----- | ----- | ----- | ----- | ------ |
  | First   | NY-store1      | 3     | 5     |  2    | 2     |  2,66  |
  | Second  | NY-store2      | 1     | 3     |  5    | 1     |  1,33  |
  | Third   | NJ-store1      | 3     | 5     |  2    | 2     |    3   |
  | Fourth  | PA-store1      | 1     | 3     |  5    | 1     |   2,5  |
  </code></pre>