title: Applying a custom groupby aggregate function to output a binary outcome in pandas python
body: <p>I have a dataset of trader transactions where the variable of interest is <code>Buy/Sell</code> which is binary and takes on the value of 1 f the transaction was a buy and 0 if it is a sell. An example looks as follows: </p>
  
  <pre><code>Trader     Buy/Sell
    A           1
    A           0
    B           1
    B           1
    B           0
    C           1
    C           0
    C           0
  </code></pre>
  
  <p>I would like to calculate the net <code>Buy/Sell</code> for each trader such that if the trader had more than 50% of trades as a buy, he would have a <code>Buy/Sell</code> of 1, if he had less than 50% buy then he would have a <code>Buy/Sell</code> of 0 and if it were exactly 50% he would have NA (and would be disregarded in future calculations).</p>
  
  <p>So for trader A, the buy proportion is (number of buys)/(total number of trader) = 1/2 = 0.5 which gives NA.</p>
  
  <p>For trader B it is 2/3 = 0.67 which gives a 1</p>
  
  <p>For trader C it is 1/3 = 0.33 which gives a 0</p>
  
  <p>The table should look like this:</p>
  
  <pre><code>Trader     Buy/Sell
    A           NA
    B           1
    C           0 
  </code></pre>
  
  <p>Ultimately i want to compute the total aggregated number of buys, which in this case is 1, and the aggregated total number of trades (disregarding NAs) which in this case is 2. I am not interested in the second table, I am just interested in the aggregated number of buys and the aggregated total number (count) of <code>Buy/Sell</code>.</p>
  
  <p>How can I do this in Pandas?</p>