title: Pandas rank by column value with conditions
body: <p>I have the following dataset <strong>(non-unique id)</strong> :</p>
  
  <pre><code>id   data  country
  1    8     B
  2    15    A
  3    14    D
  3    19    D
  3    8     C
  3    20    A
  </code></pre>
  
  <p>For rows with <strong>country ANYTHING BUT "A"</strong>, I want to add a "rank" column.</p>
  
  <p>For rows with <strong>country "A"</strong>, I want to leave "rank" value empty (or 0).</p>
  
  <p>Expected output :</p>
  
  <pre><code>id   data  country rank
  1    8     B       1
  2    15    A       0
  3    14    D       3 
  3    19    D       4
  3    8     C       2
  3    20    A       0
  </code></pre>
  
  <p>This post <a href="https://stackoverflow.com/questions/30425796/pandas-rank-by-column-value">Pandas rank by column value</a> gives great insight.</p>
  
  <p>I can try :</p>
  
  <pre><code>df['rank'] = df['data'].rank(ascending=True)
  </code></pre>
  
  <p>but I don't know how to take "country" into account ?</p>