gpt4 book ai didi

python - 数据帧中每行的相关系数和 p 值

转载 作者:行者123 更新时间:2023-12-01 03:01:06 24 4
gpt4 key购买 nike

我有一个矩阵,如下所示,

foo = pd.DataFrame(
[['ASP1',12.45,12.65,1.54,1.56],
['ASP2',4.5,1.4,0.03,1.987],
['ASP3',0.12,0.34,0.45,0.9],
['ASP4',0.65,0.789,0.01,0.876]],
columns = ('Sam','C1','C2','B1','B2'))
foo
Sam C1 C2 B1 B2
0 ASP1 12.45 12.650 1.54 1.560
1 ASP2 4.50 1.400 0.03 1.987
2 ASP3 0.12 0.340 0.45 0.900
3 ASP4 0.65 0.789 0.01 0.876

我想对 Sam 中 C1..C2 和 B1..B2 列之间的每一行进行相关性测试。最后,我的目标是生成如下矩阵,

foo_result = pd.DataFrame(
[['C',0.76,0.06],
['B',0.34,0.10]],
columns = ('Gen','Correlation_coefficent','P-value'))
foo_result

Gene Correlation_coefficent P-value
0 C 0.76 0.060
1 B 0.34 0.100

任何建议或解决方案都会很棒。谢谢

最佳答案

这应该可以做到:

from scipy.stats import pearsonr

c_values = [column for column in foo.columns.tolist() if column.startswith('C')]
b_values = [column for column in foo.columns.tolist() if column.startswith('B')]

foo['Correlation_coefficent'], foo['P-value'] = zip(*foo.T.apply(lambda x: pearsonr(x[c_values], x[b_values])))
foo_result = foo[['Sam', 'Correlation_coefficent','P-value']]

输出:

    Sam  Correlation_coefficent  P-value
0 ASP1 1.0 0.0
1 ASP2 -1.0 0.0
2 ASP3 1.0 0.0
3 ASP4 1.0 0.0

获得这些结果的原因是变量的数量。希望您的原件至少有 3 个值。

关于python - 数据帧中每行的相关系数和 p 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43844463/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com