title: Pandas DataFrame change a value based on column, index values comparison
body: <p>Suppose that you have a pandas <code>DataFrame</code> which has some kind of data in the body and <em>numbers</em> in the <code>column</code> and <code>index</code> names.</p>
  
  <pre><code>&gt;&gt;&gt; data=np.array([['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']])
  &gt;&gt;&gt; columns = [2, 4, 8]
  &gt;&gt;&gt; index = [10, 4, 2]
  &gt;&gt;&gt; df = pd.DataFrame(data, columns=columns, index=index)
  &gt;&gt;&gt; df
      2  4  8
  10  a  b  c
  4   d  e  f
  2   g  h  i
  </code></pre>
  
  <p>Now suppose we want to manipulate are data frame in some kind of way based on comparing the index and columns. Consider the following.</p>
  
  <blockquote>
    <p>Where index is greater than column replace letter with 'k':</p>
  </blockquote>
  
  <pre><code>    2  4  8
  10  k  k  k
  4   k  e  f
  2   g  h  i
  </code></pre>
  
  <blockquote>
    <p>Where index is equal to column replace letter with 'U':</p>
  </blockquote>
  
  <pre><code>    2  4  8
  10  k  k  k
  4   k  U  f
  2   U  h  i
  </code></pre>
  
  <blockquote>
    <p>Where column is greater than index replace letter with  'Y':</p>
  </blockquote>
  
  <pre><code>    2  4  8
  10  k  k  k
  4   k  U  Y
  2   U  Y  Y
  </code></pre>
  
  <p>To keep the question useful to all:</p>
  
  <ul>
  <li><p>What is a fast way to do this replacement?</p></li>
  <li><p>What is the simplest way to do this replacement?</p></li>
  </ul>
  
  <p><strong>Speed Results from minimal example</strong></p>
  
  <ul>
  <li><p><strong><em>jezrael</em></strong>: <code>556 µs ± 66.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)</code></p></li>
  <li><p><strong><em>user3471881</em></strong>: <code>329 µs ± 11.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)</code></p></li>
  <li><p><strong><em>thunderwood</em></strong>: <code>4.65 ms ± 252 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)</code></p></li>
  </ul>
  
  <hr>
  
  <p><strong>Is this a duplicate?</strong>
  I searched google for <code>pandas replace compare index column</code> and the top results are:</p>
  
  <p><a href="https://stackoverflow.com/questions/47571309/pandas-compare-two-dataframes-and-replace-values-matching-condition">Pandas - Compare two dataframes and replace values matching condition</a></p>
  
  <p><a href="https://stackoverflow.com/questions/38776699/python-pandas-replace-values-based-on-location-not-index-value">Python pandas: replace values based on location not index value</a></p>
  
  <p><a href="https://stackoverflow.com/questions/31511997/pandas-dataframe-replace-all-values-in-a-column-based-on-condition">Pandas DataFrame: replace all values in a column, based on condition</a></p>
  
  <p>However, I don't feel any of these touch on whether this a) possible or b) how to compare in such a way</p>