You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求协助编写Google Colab代码筛选CSV中基因相关性数据

解决方案:基因相关性分析与筛选(Google Colab)

1. 数据读取与基础处理

首先导入依赖库,读取CSV并保留需要的前两列:

import pandas as pd
import numpy as np

# 读取数据集(替换为你的CSV路径,Colab可上传文件后直接用文件名)
df = pd.read_csv("你的数据集.csv", usecols=[0, 1])
# 确保列名准确(如果原列名不符合,可手动重命名)
df.columns = ["Feature", "Correlation"]

2. 解决显示问题

针对你提到的无法显示全部基因和小数截断问题,调整pandas的显示配置:

# 显示所有行(取消行数限制)
pd.set_option('display.max_rows', None)
# 设置小数显示格式(这里保留8位,可按需调整)
pd.set_option('display.float_format', '{:.8f}'.format)

3. 标记相关性类型

添加列自动判断基因的相关性类别:

# 新增相关性分类列
df["Correlation_Type"] = np.where(
    df["Correlation"] > 0.95, "强正相关",
    np.where(df["Correlation"] < -0.95, "强负相关", "无相关")
)

4. 筛选指定基因

封装函数快速查询目标基因的相关性信息:

def query_gene(gene_name):
    # 精准匹配目标基因
    result = df[df["Feature"] == gene_name]
    if result.empty:
        print(f"未找到基因 {gene_name}")
    else:
        print(f"\n{gene_name} 相关性分析结果:")
        print(result)

# 示例调用:查询TP53的相关性
query_gene("TP53")

完整可运行代码

把所有步骤整合,直接在Colab中运行:

import pandas as pd
import numpy as np

# 读取数据
df = pd.read_csv("gene_correlation.csv", usecols=[0, 1])
df.columns = ["Feature", "Correlation"]

# 配置显示
pd.set_option('display.max_rows', None)
pd.set_option('display.float_format', '{:.8f}'.format)

# 标记相关性类型
df["Correlation_Type"] = np.where(
    df["Correlation"] > 0.95, "强正相关",
    np.where(df["Correlation"] < -0.95, "强负相关", "无相关")
)

# 查看全量结果(可选)
print("全量基因相关性分析:")
print(df)

# 筛选指定基因
def query_gene(gene_name):
    result = df[df["Feature"] == gene_name]
    if result.empty:
        print(f"\n未找到基因 {gene_name}")
    else:
        print(f"\n{gene_name} 相关性信息:")
        print(result)

# 测试查询
query_gene("TP53")
query_gene("EGFR")

关键说明

  • 显示问题:通过修改pandas的max_rows和float_format参数,解决行数限制和小数截断问题
  • 相关性分类:用np.where实现高效的多条件判断,比循环更适合大数据集
  • 基因筛选:通过布尔索引df["Feature"] == gene_name精准匹配目标基因,封装函数后调用更便捷

内容的提问来源于stack exchange,提问作者Ernie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 11:00:02