title: Pandas groupby count with conditions
body: <h3>Example Data</h3>
  
  <p>Given the following data frame: </p>
  
  <pre><code>| feature | gene  |  target  | pos | 
  | 1_1_1   | NRAS  |  AATTGG  | 60  |
  | 1_1_1   | NRAS  |  TTGGCC  | 6   |
  | 1_1_1   | NRAS  |  AATTGG  | 20  |
  | 1_1_1   | KRAS  |  GGGGTT  |  0  |
  | 1_1_1   | KRAS  |  GGGGTT  |  0  |
  | 1_1_1   | KRAS  |  GGGGTT  |  0  |
  | 1_1_2   | NRAS  |  CCTTAA  | 2   |
  | 1_1_2   | NRAS  |  GGAATT  | 8   |
  | 1_1_2   | NRAS  |  AATTGG  | 60  |
  </code></pre>
  
  <h3>The problem</h3>
  
  <p>For each feature, I would like to count how many targets appear in each gene, with the following rules: </p>
  
  <ul>
  <li>If a target appears in only one position (pos column) for each gene, it gets a count of 1 for every time seen</li>
  <li>If the same target appears in multiple positions for each gene, it gets a count of (count at position/total positions found)</li>
  <li>Summarize total counts of each gene per feature</li>
  </ul>
  
  <h3>What I've done so far</h3>
  
  <pre><code>matches.groupby(["FeatureID", "gene"]).size().reset_index()
  
  matches['multi_mapped'] = np.where(matches.groupby(["FeatureID", "gene", "target"]).pos.transform('nunique') &gt; 1, "T", '')
  </code></pre>
  
  <p>Which gives me a dataframe where targets that appear at more than one position are flagged as true. Now I just need to figure out how to normalize the counts.  </p>
  
  <h3>Desired output</h3>
  
  <pre><code>| feature | gene  |  count
  | 1_1_1   | NRAS  |   2
  | 1_1_1   | KRAS  |   1
  | 1_1_2   | NRAS  |   3
  </code></pre>
  
  <p>So in the example above for 1_1_1 NRAS, where AATTGG is found at both position 60 and position 20, each would get a count of .5. Since TTGGCC was found once at one position, so it gets a count of 1. This makes a total count of 2. </p>
  
  <p>If for 1_1_1 NRAS TTGGCC was found 3 times at the same position, each of those would get a count of 1, for a total of 3 + .5 + .5 = 4. </p>
  
  <p>The solution needs to check for the same target appearing at different positions and then adjust the counts accordingly, and that is the part I'm having a difficult time with. My ultimate goal is to choose the gene with the highest count per group. </p>