小编Dav*_*ell的帖子

如何制作更高效的代码来搜索熊猫列中的多个字符串

我是一名新近自学成才的(减去 1 节非常基础的课程)程序员,在生物实验室工作。我有一个脚本,它遍历来自两种不同细胞类型的 RNAseq 数据,并在另一个数据集中运行 ttest。它适用于这个应用程序,但代码感觉非常粗鲁,我知道我会写很多类似的脚本。

如何更好地编写以下代码以使其更高效?

计划目标:

  1. 将基因列表与两种细胞类型的 rnaseq 文库进行比较,如果该文库包含该基因,则运行细胞类型 1 与细胞类型 2 的 ttest
  2. 输出结果。

import pandas as pd
from scipy.stats import ttest_ind
rnatest = {'Gene symbol':["GeneA","GeneB"],"rnaseq1A":[1,1.5],"rnaseq1B":[1.3,1.2],"rnaseq2A":[2.3,2.7],"rnaseq2B":[2,2.6]} 
df = pd.DataFrame(rnatest)
GOIlist = ["GeneA","GeneB"]
GOI = []
mu = [] 
pval = []
for index, row in df.iterrows():
  if row['Gene symbol'] in GOIlist:
    t, p = ttest_ind([row["rnaseq1A"],row["rnaseq1B"]],[row["rnaseq2A"],row["rnaseq2B"]])
    GOI.append(row['Gene symbol'])
    mu.append(t)
    pval.append(p)
df2 = {'Gene symbol':GOI,"tVAL":mu, "pVAL":pval}
df2 = pd.DataFrame(df2)
print(df2)  
Run Code Online (Sandbox Code Playgroud)

python bioinformatics biopython pandas

5
推荐指数
1
解决办法
86
查看次数

标签 统计

bioinformatics ×1

biopython ×1

pandas ×1

python ×1