import pandas as pd
# 读取csv文件
data = pd.read_csv('filename.csv')
# 创建一个新的DataFrame存储结果
result = pd.DataFrame(columns=['SNP', 'Best Genotype', 'Phenotype'])
# 对于每个SNP
for snp in range(2, len(data.columns)):
# 提取SNP基因型数据
genotypes = data.iloc[:, snp].tolist()
# 筛选纯和基因型
pure_genotypes = [g for g in genotypes if g in ['AA', 'BB']]
# 跳过所有杂合基因型的情况
if len(pure_genotypes) == 0:
continue
# 获取该SNP纯和基因型对应的表型值
phenotype = [data.iloc[i, 1] for i in range(len(genotypes)) if genotypes[i] in ['AA', 'BB']]
# 计算每个纯和基因型的表型平均值
AA_phenotype = sum([phenotype[i] for i in range(len(phenotype)) if pure_genotypes[i] == 'AA']) / len([g for g in pure_genotypes if g=='AA'])
BB_phenotype = sum([phenotype[i] for i in range(len(phenotype)) if pure_genotypes[i] == 'BB']) / len([g for g in pure_genotypes if g=='BB'])
# 找到表现最好的基因型
best_genotype = 'AA' if AA_phenotype > BB_phenotype else 'BB'
best_phenotype = AA_phenotype if AA_phenotype > BB_phenotype else BB_phenotype
# 将这些信息添加到结果DataFrame中
result.loc[snp-2] = [data.columns[snp], best_genotype, best_phenotype]
# 将结果保存到文件
result.to_csv('best_genotypes.csv', index=False)