【问题标题】:If list in a column of Pandas DataFrame如果在 Pandas DataFrame 的列中列出
【发布时间】:2022-11-13 05:00:50
【问题描述】:

我正在尝试验证列表的元素是否包含在 Pandas 的 DataFrame (DF) 中。

这是我到目前为止的代码:

from pathlib import Path

data = pd.read_excel(r'/home/darteagam/diploma/bert/files/codon_positions.xlsx')
df = pd.DataFrame(data,columns=['position','codon','aminoacid'])
print("DataFrame Loaded!")
#print(df)

# reading the files

with open("/home/darteagam/diploma/bert/files/bert_aa_example.txt", "r") as f1, open("/home/darteagam/diploma/bert/files/bert_nn_example.txt", "r") as f2:
    #with open("/home/darteagam/diploma/bert/files/bert_nn_example.txt", "r") as f2:
    print('AA in 31 position:')
    for aa in f1:
        prot_seq = list(aa)
        lp = len(prot_seq)
        position_aa = prot_seq[30:31]
        #print(prot_seq)
        position_aa = list(aa[30:31]) # verifiying the 31 position
        print(position_aa)
        #print(len(position_aa))
        #print(aa)
    #print('Nucleotide sequences')
    for nn in f2:
        nuc_seq = nn
        #print(nuc_seq)
        x=3 
        spl=[nuc_seq[y-x:y] for y in range(x, len(nuc_seq)+x,x)]
        pos_cod = spl[30:31]
        list_codons = (list(pos_cod))
        print(list_codons)
        #print(len(list_codons))
        #print(spl)

列表的输出:

['ATC']
['AAC']
['ACC']
['TTT']
['GTC']
['CTC']

DF的输出:

         position codon aminoacid
0          1   GCT         A
1          2   GCC         A
2          3   GCA         A
3          4  GCG          A
4          5   CGT         R
..       ...   ...       ...
56        57   TAC         Y
57        58  GTT          V
58        59  GTC          V
59        60  GTA          V
60        61   GTG         V

我想验证输出中的列表是否包含在 DF 的列密码子中,并获取该元素在 DF 中的位置。

任何建议都会很有用,因为这是我第一次使用 Pandas。

【问题讨论】:

    标签: python pandas list dataframe machine-learning


    【解决方案1】:

    首先,目前您提供的“输出”似乎是一系列标准输出。最好有一个像['ATC','AAC','ACC','TTT','GTC','CTC'] 这样的列表。

    具体来说,我怀疑对第二个循环的以下更改会产生这样的列表。

        # <first for loop>...
        #
        codon_list = []
        for nuc_seq in f2:
            x=3 
            spl=[nuc_seq[y-x:y] for y in range(x, len(nuc_seq)+x,x)]
            
            # pos_cod in your code is a list containing only spl[30]
            codon_list.append(spl[30]) 
    

    获得此列表后,您可以执行以下操作:

    import pandas as pd
    
    # generate example dataframe
    df = pd.DataFrame({'position': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 56: 57, 57: 58, 58: 59, 59: 60, 60: 61}, 'codon': {0: 'GCT', 1: 'GCC', 2: 'GCA', 3: 'GCG', 4: 'CGT', 56: 'TAC', 57: 'GTT', 58: 'GTC', 59: 'GTA', 60: 'GTG'}, 'aminoacid': {0: 'A', 1: 'A', 2: 'A', 3: 'A', 4: 'R', 56: 'Y', 57: 'V', 58: 'V', 59: 'V', 60: 'V'}})
    
    # example list of codons
    codon_list = ['TTT','GTC','CTC','GCC']
    
    for c in codon_list:
        where = (df['codon'] == c)
        if where.any():
            pos = df.at[where.idxmax(), 'position']
            print(f"codon {c}: position {pos}")
        else:
            print(f"codon {c} not found")
    

    生成的示例数据框:

        position codon aminoacid
    0          1   GCT         A
    1          2   GCC         A
    2          3   GCA         A
    3          4   GCG         A
    4          5   CGT         R
    56        57   TAC         Y
    57        58   GTT         V
    58        59   GTC         V
    59        60   GTA         V
    60        61   GTG         V
    

    脚本的打印结果:

    codon TTT not found
    codon GTC: position 59
    codon CTC not found
    codon GCC: position 2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-08
      • 1970-01-01
      • 1970-01-01
      • 2018-04-07
      • 2016-09-01
      • 2015-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多