By Gengxin, 31 October, 2025
Forums

根据基因的表达量文件求每个基因的平均值

import pandas as pd

# Load the CSV data
df = pd.read_csv('female_tpm.csv')

# Calculate the mean for each gene across all columns
mean_values = df.iloc[:, 1:].mean(axis=1)

# Create a new dataframe with only gene_id and mean_tpm
result_df = pd.DataFrame({
    'gene_id': df['gene_id'],
    'female_tpm': mean_values
})

# Save the new dataframe to a new CSV file
result_df.to_csv('female_tpm_with_mean.csv', index=False)

print("Results saved to 'female_tpm_with_mean.csv'")

import pandas as pd

# Load the CSV data
df = pd.read_csv('male_tpm.csv')

# Calculate the mean for each gene across all columns
mean_values = df.iloc[:, 1:].mean(axis=1)

# Create a new dataframe with only gene_id and mean_tpm
result_df = pd.DataFrame({
    'gene_id': df['gene_id'],
    'male_tpm': mean_values
})

# Save the new dataframe to a new CSV file
result_df.to_csv('male_tpm_with_mean.csv', index=False)

print("Results saved to 'male_tpm_with_mean.csv'")

【金山文档 | WPS云文档】 基因的表达量求平均值代码