title: Key error when dividind one row by another in Pandas
body: <p>I'm trying to perform some calculations across a complex data set and struggling to get the right approach. In simple terms, what I would like to do is divide a value in one row, by a value in another row. A sample of what I mean is as follows;</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th></th>
  <th>Column A</th>
  <th>Values</th>
  <th>Column B</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>0</td>
  <td>row 1</td>
  <td>5</td>
  <td>abc</td>
  </tr>
  <tr>
  <td>1</td>
  <td>row 2</td>
  <td>10</td>
  <td>def</td>
  </tr>
  <tr>
  <td>2</td>
  <td>row 3</td>
  <td>15</td>
  <td>ghi</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>The output that I desire, is to divide row1 by row2, giving me 0.5, or 50% and place this data in a new row (row 4). I essentially want to ignore, Column B and row 3 in this scenario. I still want their data in the output, but they are not involved in the calculation. Output should look like this, or similar.</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th></th>
  <th>Column A</th>
  <th>Values</th>
  <th>Column B</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>0</td>
  <td>row 1</td>
  <td>5</td>
  <td>abc</td>
  </tr>
  <tr>
  <td>1</td>
  <td>row 2</td>
  <td>10</td>
  <td>def</td>
  </tr>
  <tr>
  <td>2</td>
  <td>row 3</td>
  <td>15</td>
  <td>ghi</td>
  </tr>
  <tr>
  <td>3</td>
  <td>row 4</td>
  <td>0.5</td>
  <td>NaN</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>This is a simplified version, as I'll have a large number of rows and columns in reality, however the concept is the same. I want to tell pandas which two rows to divide, multiply etc and put it into a new row.</p>
  <p>This should be fairly simple, using the following code that I found;</p>
  <pre><code>df['row4']=df.loc[row 1]/df.loc[row.2]
  </code></pre>
  <p>However this returns a key error on Row 1</p>
  <pre><code>KeyError                                  Traceback (most recent call last)
  &lt;ipython-input-78-c216003d88c4&gt; in &lt;module&gt;
  ----&gt; 1 df['row4']=df.loc['row 1']/df.loc['row 2']
  
  ~\anaconda3\lib\site-packages\pandas\core\indexing.py in __getitem__(self, key)
      893 
      894             maybe_callable = com.apply_if_callable(key, self.obj)
  --&gt; 895             return self._getitem_axis(maybe_callable, axis=axis)
      896 
      897     def _is_scalar_access(self, key: Tuple):
  
  ~\anaconda3\lib\site-packages\pandas\core\indexing.py in _getitem_axis(self, key, axis)
     1122         # fall thru to straight lookup
     1123         self._validate_key(key, axis)
  -&gt; 1124         return self._get_label(key, axis=axis)
     1125 
     1126     def _get_slice_axis(self, slice_obj: slice, axis: int):
  
  ~\anaconda3\lib\site-packages\pandas\core\indexing.py in _get_label(self, label, axis)
     1071     def _get_label(self, label, axis: int):
     1072         # GH#5667 this will fail if the label is not present in the axis.
  -&gt; 1073         return self.obj.xs(label, axis=axis)
     1074 
     1075     def _handle_lowerdim_multi_index_axis0(self, tup: Tuple):
  
  ~\anaconda3\lib\site-packages\pandas\core\generic.py in xs(self, key, axis, level, drop_level)
     3737                 raise TypeError(f&quot;Expected label or tuple of labels, got {key}&quot;) from e
     3738         else:
  -&gt; 3739             loc = index.get_loc(key)
     3740 
     3741             if isinstance(loc, np.ndarray):
  
  ~\anaconda3\lib\site-packages\pandas\core\indexes\range.py in get_loc(self, key, method, tolerance)
      352                 except ValueError as err:
      353                     raise KeyError(key) from err
  --&gt; 354             raise KeyError(key)
      355         return super().get_loc(key, method=method, tolerance=tolerance)
      356 
  
  KeyError: 'row 1'
  </code></pre>
  <p>Any help is very welcome!</p>