首页 > 解决方案 > Python pandas 特定的标头检查器

问题描述

我写了一个从 excel 文件中获取数据的 pandas python 代码。这是我正在使用的示例代码。但是一些excel文件没有一些标题,Abundance: F1: 127C, Sample因此我如何检查计算。我正在考虑尝试除循环,但我不知道如何在可用循环之后列出。

非常感谢你。

# Here I am reading the excel file
Data=pd.read_excel("example.xlsx", header=0)

# Here I am taking the specific headers as list from the excel file
AccessionNum = list(Data['Accession'])
N_127_L = list(Data['Abundance: F1: 127N, Sample'])
C_127_L = list(Data['Abundance: F1: 127C, Sample'])
N_128_L = list(Data['Abundance: F1: 128N, Sample'])

C_128_L = list(Data['Abundance: F1: 128C, Sample'])
N_129_L = list(Data['Abundance: F1: 129N, Sample'])
C_129_L = list(Data['Abundance: F1: 129C, Sample'])

N_130_L = list(Data['Abundance: F1: 130N, Sample'])
C_130_L = list(Data['Abundance: F1: 130C, Sample'])
N_131_L = list(Data['Abundance: F1: 131N, Sample'])

j = 0
for i in AccessionNum:
# Here I am iterating the all of the rows
    N_127 = N_127_L[j]
    C_127 = C_127_L[j]
    N_128 = N_128_L[j]

    C_128 = C_128_L[j]
    N_129 = N_129_L[j]
    C_129 = C_129_L[j]

    N_130 = N_130_L[j]
    C_130 = C_130_L[j]
    N_131 = N_131_L[j]

# Here I am grouping my columns but sometimes one or two doesn't have
    dmsoList = [N_127, C_127, N_128]
    drug1List = [C_128, N_129, C_129]
    drug2List = [N_130, C_130, N_131]

# Here I am doing some calculation which is not important
    dmso = statistics.mean(dmsoList)
    drug1 = statistics.mean(drug1List)
    drug2 = statistics.mean(drug2List)

    log2_drug1 = '=LOG({}/{},2)'.format(drug1, dmso)
    log2_drug2 = '=LOG({}/{},2)'.format(drug2, dmso)

    ttest_drug1 = stats.ttest_ind(dmsoList, drug1List, equal_var=True)
    ttest_drug2 = stats.ttest_ind(dmsoList, drug2List, equal_var=True)
    log10_ttest_drug1 = '=-LOG({})'.format(ttest_drug1[1])
    log10_ttest_drug2 = '=-LOG({})'.format(ttest_drug2[1])

    Data.loc[j, 'Log2 Drug_1'] = log2_drug1
    Data.loc[j, 'Log2 Drug_2'] = log2_drug2
    Data.loc[j, '-log10 Drug_1 t-test'] = log10_ttest_drug1
    Data.loc[j, '-log10 Drug_2 t-test'] = log10_ttest_drug2
    j += 1

# Here I am outputting the data as excel file again
Data.to_excel('_Calculation.xlsx')

数据示例

标签: pythonexcelpandascalculation

解决方案


推荐阅读