title: Best way to assign value on condition in Pandas
body: <p>What is the correct way to identify max value in subgroup and assign each row value based on whether it is max or not?
  Here is an example df:</p>
  
  <pre><code>group            subgroup
    A                 1
    B                 1
    A                 2
    A                 3
    A                 4
    B                 2
    C                 2
    C                 1
  </code></pre>
  
  <p>The rules are:</p>
  
  <pre><code>if subgroup = max then result = 1
  else subgroup = 2
  </code></pre>
  
  <p>Result will be:</p>
  
  <pre><code>group            subgroup      result
    A                 1            2
    B                 1            2
    A                 2            2
    A                 3            2
    A                 4            1
    B                 2            1
    C                 2            1
    C                 1            2
  </code></pre>
  
  <p>I do it this way now:</p>
  
  <pre><code>df['subgroup_max'] = df.groupby(['group'])['subgroup'].nunique()
  df3['result'] = 2
  df3.loc[df3['result'] == df3['subgroup_max'],'result'] = 1
  </code></pre>
  
  <p>Doesn't seem to be very efficient. But is there a better way?</p>