'Calculate mean and std of a file with gene expression
I have a task where I'm supposed to calculate the mean and std of the expressions levels for each gene (from a file). The calculations should be performed for separate groups and the results are going to be written to a new file. The file should be tab-separated and contain 5 columns: Gene ID, Control mean, Control stdev, Patient mean and Patient stdev. I can't seem to get the hand of this and I've written something but it dosen't work. Can someone please help me?
import statistics
import pandas as pd
dict={}
dict=pd.read_csv('MS_expression.tsv', sep='\t')
def calculate_mean_std(samples):
samples_mean = statistics.mean(samples)
samples_std = statistics.stdev(samples)
return samples_mean, samples_std
with open('Q3.txt', 'w') as f:
#Start with writing heading to the file
f.write('symbol\control mean\contorl stdv\patient mean\patient stdev')
#Iterate over the items to both access the key and value.
for SYMBOL, control in dict.items():
control_mean, control_std=calculate_mean_std(control)
f.write(SYMBOL+ '\t')
f.write(str(round(control_mean, 2)) + '\t')
f.write(str(round(control_std, 2)) + '\t')```
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
