在 pandas 中,如何在具有匹配行和列的 3 个单独数据帧之间建立相关矩阵?

Posted

技术标签:

【中文标题】在 pandas 中,如何在具有匹配行和列的 3 个单独数据帧之间建立相关矩阵?【英文标题】:In pandas, how do you make a correlation matrix between 3 separate dataframes with matching rows and columns? 【发布时间】:2020-12-25 14:39:12 【问题描述】:

每个数据框都有基因名称的行索引和细胞系的列索引,每个细胞都有表达水平。这 3 个数据框具有相同的相应基因名称和细胞系,我想找到相应行的三联体之间的相关性(即细胞系如何影响 3 个数据框之间每个特定基因的表达)。我如何在新数据框中找到相关系数,然后使用热图将其可视化?

谢谢!

DATAFRAME1 = pd.DataFrame("GENENAME":[GENE1,GENE2,GENE3,GENE4,GENE5],"CELLLINE1":[34,12,78,84,26], "CELLLINE2":[54,87,35,25,82], "CELLLINE3":[56,78,0,14,13], "CELLLINE4":[0,23,72,56,14], "CELLLINE5":[78,12,31,0,34])
DATAFRAME2 = pd.DataFrame("GENENAME":[GENE1,GENE2,GENE3,GENE4,GENE5],"CELLLINE1":[45,24,65,65,65], "CELLLINE2":[45,87,65,52,12], "CELLLINE3":[98,52,32,32,12], "CELLLINE4":[0,23,1,365,53], "CELLLINE5":[24,12,65,3,65])
DATAFRAME3 = pd.DataFrame("GENENAME":[GENE1,GENE2,GENE3,GENE4,GENE5],"CELLLINE1":[14,96,25,2,25], "CELLLINE2":[47,7,5,58,34], "CELLLINE3":[85,45,65,53,53], "CELLLINE4":[3,35,12,56,236], "CELLLINE5":[68,10,45,46,85])

【问题讨论】:

您能给我们提供样本数据框吗? @BillyBonaros 感谢您的建议。我加了一个 @BillyBonaros 基因名称是我试图用作行索引的字符串 【参考方案1】:

如果我理解正确,您可以执行以下操作:

DATAFRAME1 = pd.DataFrame("GENENAME":['GENE1','GENE2','GENE3','GENE4','GENE5'],"CELLLINE1":[34,12,78,84,26], "CELLLINE2":[54,87,35,25,82], "CELLLINE3":[56,78,0,14,13], "CELLLINE4":[0,23,72,56,14], "CELLLINE5":[78,12,31,0,34])
DATAFRAME2 = pd.DataFrame("GENENAME":['GENE1','GENE2','GENE3','GENE4','GENE5'],"CELLLINE1":[45,24,65,65,65], "CELLLINE2":[45,87,65,52,12], "CELLLINE3":[98,52,32,32,12], "CELLLINE4":[0,23,1,365,53], "CELLLINE5":[24,12,65,3,65])
DATAFRAME3 = pd.DataFrame("GENENAME":['GENE1','GENE2','GENE3','GENE4','GENE5'],"CELLLINE1":[14,96,25,2,25], "CELLLINE2":[47,7,5,58,34], "CELLLINE3":[85,45,65,53,53], "CELLLINE4":[3,35,12,56,236], "CELLLINE5":[68,10,45,46,85])

df1=DATAFRAME1.set_index("GENENAME").T
df2=DATAFRAME2.set_index("GENENAME").T
df3=DATAFRAME3.set_index("GENENAME").T

现在对于每个基因,您可以执行以下操作:

pd.concat([df1[['GENE1']],df2[['GENE1']],df3[['GENE1']]],axis=1).corr()

GENENAME     GENE1     GENE1     GENE1
GENENAME                              
GENE1     1.000000  0.449474  0.843977
GENE1     0.449474  1.000000  0.695770
GENE1     0.843977  0.695770  1.000000

对于所有基因,您可以执行以下操作:

for i in DATAFRAME1['GENENAME']:
    print(i)
    print(pd.concat([df1[[i]],df2[[i]],df3[[i]]],axis=1).corr())
    print("="*50)

GENE1
GENENAME     GENE1     GENE1     GENE1
GENENAME                              
GENE1     1.000000  0.449474  0.843977
GENE1     0.449474  1.000000  0.695770
GENE1     0.843977  0.695770  1.000000
==================================================
GENE2
GENENAME     GENE2     GENE2     GENE2
GENENAME                              
GENE2     1.000000  0.932963 -0.373474
GENE2     0.932963  1.000000 -0.335923
GENE2    -0.373474 -0.335923  1.000000
==================================================
GENE3
GENENAME     GENE3     GENE3     GENE3
GENENAME                              
GENE3     1.000000 -0.113161 -0.690468
GENE3    -0.113161  1.000000  0.012654
GENE3    -0.690468  0.012654  1.000000
==================================================
GENE4
GENENAME     GENE4     GENE4     GENE4
GENENAME                              
GENE4     1.000000  0.454716 -0.694046
GENE4     0.454716  1.000000  0.230386
GENE4    -0.694046  0.230386  1.000000
==================================================
GENE5
GENENAME     GENE5     GENE5     GENE5
GENENAME                              
GENE5     1.000000 -0.392969 -0.439636
GENE5    -0.392969  1.000000  0.293649
GENE5    -0.439636  0.293649  1.000000
==================================================

【讨论】:

非常感谢您的帮助。有没有办法让每一个基因都自动化,而不是只做 GENE1?

以上是关于在 pandas 中,如何在具有匹配行和列的 3 个单独数据帧之间建立相关矩阵?的主要内容,如果未能解决你的问题,请参考以下文章

选定行和列的 Pandas min()

具有多个固定行和列的 HTML 表格

如何过滤掉单个项目,而不仅仅是在行和列的基础上?

Pandas 多索引行和列:用匹配行中的值替换 NaN

如何用Jquery获取Table指定行中指定列的数值

使用 django html 模板创建具有动态行和列的表