<p>The code is fine. The only issue is you'd put \s characters in-between the column header names. I've place an additional &quot;mutation_frequency&quot; column because thats how I'd do it.</p>
<pre><code>df = pd.read_csv('/pathtodir/test/bioinfo.csv',sep=&quot;\s&quot;,engine='python')
df = df.groupby(&quot;Genename&quot;).count()
df['Mutation_Frequency1'] = ((df[&quot;MUTATION_ID&quot;] / (df[&quot;Sampleid&quot;] + (df[&quot;Sampleid&quot;] - df[&quot;MUTATION_ID&quot;]))) * 100).round(2)
df['Mutation_Frequency2'] = (df[&quot;MUTATION_ID&quot;] / df[&quot;Sampleid&quot;])*100
df.drop(['Description', 'Mutation'], axis=1, inplace=True)
print(df)
</code></pre>
<div class="s-table-container">
<table class="s-table">
<thead>
<tr>
<th>-</th>
<th>Sampleid</th>
<th>MUTATION_ID</th>
<th>Mutation_Frequency1</th>
<th>Mutation_Frequency2</th>
</tr>
</thead>
<tbody>
<tr>
<td>ARID1B</td>
<td>5</td>
<td>3</td>
<td>42.86</td>
<td>60.0</td>
</tr>
<tr>
<td>CD58</td>
<td>6</td>
<td>3</td>
<td>33.33</td>
<td>50.0</td>
</tr>
<tr>
<td>MRE11</td>
<td>1</td>
<td>1</td>
<td>100.00</td>
<td>100.0</td>
</tr>
<tr>
<td>MRE12</td>
<td>1</td>
<td>1</td>
<td>100.00</td>
<td>100.0</td>
</tr>
<tr>
<td>MRE13</td>
<td>1</td>
<td>0</td>
<td>0.00</td>
<td>0.0</td>
</tr>
<tr>
<td>MRE14</td>
<td>1</td>
<td>1</td>
<td>100.00</td>
<td>100.0</td>
</tr>
<tr>
<td>MRE15</td>
<td>1</td>
<td>0</td>
<td>0.00</td>
<td>0.0</td>
</tr>
<tr>
<td>MRE16</td>
<td>1</td>
<td>1</td>
<td>100.00</td>
<td>100.0</td>
</tr>
</tbody>
</table>
</div>
CC BY-SA 4.0
2023-02-28T22:17:41.100Z
2023-03-01T01:21:36.913Z
2023-03-01T01:21:36.913Z
M__
<p>I have a dataset for mutation data and I want to calculate mutation frequencies across all genes</p>
<p><strong>df</strong> (This is only the small subset of data)</p>
<pre><code>Gene name   Sample id   MUTATION_ID Mutation Description
ARID1B  2719660 171258500   Substitution - Missense
ARID1B  2719659     
ARID1B  2719661 171258501   Substitution - Missense
ARID1B  2719662     
ARID1B  2719663 171258501   Substitution - Nonsense
CD58    2878555 110346783   Substitution - Nonsense
CD58    2877956     
CD58    2878557     
CD58    2877958 110346784   Substitution - Nonsense
CD58    2878559 110346785   Substitution - Nonsense
CD58    2877960     
MRE11   2861617 123320443   Substitution - coding silent
MRE12   2861617 123320444   Substitution - coding 
MRE13   2861617     
MRE14   2861617 123320445   Substitution - coding silent
MRE15   2861617     
MRE16   2861617 123320446   Substitution - coding 
</code></pre>
<p>The formula for calculating the mutation is</p>
<p><code>Positives ÷ (Positives + Negatives) x 100</code></p>
<p>where,</p>
<p><code>Positives</code>  = No of samples where <code>MUTATION_ID</code> is present</p>
<p><code>Negative</code> = No of samples <code>MUTATION_ID</code> for the sample</p>
<p>I want to calculate mutation frequency for every gene in the <code>column_1:Gene name</code> with python script</p>
<p>I tried the following code</p>
<pre><code>df = df.groupby(&quot;Gene name&quot;).count()
Positives = df[&quot;MUTATION_ID&quot;]
Negatives = df[&quot;Sample id&quot;] - df[&quot;MUTATION_ID&quot;] 
df['Mutation_Frequency'] = Positives / (Positives + Negatives) * 100
</code></pre>

CC BY-SA 4.0
2023-03-01T10:12:42.810Z
Are your fields tab separated? They probably have to be since you have spaces in the description field. And what are those extra `-`? Is that a separate field with no header?
terdon
<python><mutations><pandas>
calculating mutation frequencies for every gene
# calculating mutation frequencies for every gene

(Asked by: username_0 on 2023-02-28)

I have a dataset for mutation data and I want to calculate mutation frequencies across all genes
df (This is only the small subset of data)
Gene name   Sample id   MUTATION_ID Mutation Description
ARID1B  2719660 171258500   Substitution - Missense
ARID1B  2719659     
ARID1B  2719661 171258501   Substitution - Missense
ARID1B  2719662     
ARID1B  2719663 171258501   Substitution - Nonsense
CD58    2878555 110346783   Substitution - Nonsense
CD58    2877956     
CD58    2878557     
CD58    2877958 110346784   Substitution - Nonsense
CD58    2878559 110346785   Substitution - Nonsense
CD58    2877960     
MRE11   2861617 123320443   Substitution - coding silent
MRE12   2861617 123320444   Substitution - coding 
MRE13   2861617     
MRE14   2861617 123320445   Substitution - coding silent
MRE15   2861617     
MRE16   2861617 123320446   Substitution - coding 

The formula for calculating the mutation is
Positives ÷ (Positives + Negatives) x 100
where,
Positives  = No of samples where MUTATION_ID is present
Negative = No of samples MUTATION_ID for the sample
I want to calculate mutation frequency for every gene in the column_1:Gene name with python script
I tried the following code
df = df.groupby("Gene name").count()
Positives = df["MUTATION_ID"]
Negatives = df["Sample id"] - df["MUTATION_ID"] 
df['Mutation_Frequency'] = Positives / (Positives + Negatives) * 100

**Tags:** python, mutations, pandas
> **Comments:**
> **username_1**: Are your fields tab separated? They probably have to be since you have spaces in the description field. And what are those extra `-`? Is that a separate field with no header?  

---

## 1 answers

### Answer by username_2 on 2023-02-28. Score: 2 | Accepted answer

The code is fine. The only issue is you'd put \s characters in-between the column header names. I've place an additional "mutation_frequency" column because thats how I'd do it.
df = pd.read_csv('/pathtodir/test/bioinfo.csv',sep="\s",engine='python')
df = df.groupby("Genename").count()
df['Mutation_Frequency1'] = ((df["MUTATION_ID"] / (df["Sampleid"] + (df["Sampleid"] - df["MUTATION_ID"]))) * 100).round(2)
df['Mutation_Frequency2'] = (df["MUTATION_ID"] / df["Sampleid"])*100
df.drop(['Description', 'Mutation'], axis=1, inplace=True)
print(df)





-
Sampleid
MUTATION_ID
Mutation_Frequency1
Mutation_Frequency2




ARID1B
5
3
42.86
60.0


CD58
6
3
33.33
50.0


MRE11
1
1
100.00
100.0


MRE12
1
1
100.00
100.0


MRE13
1
0
0.00
0.0


MRE14
1
1
100.00
100.0


MRE15
1
0
0.00
0.0


MRE16
1
1
100.00
100.0


---

