如何在python中将一列拆分为两列?

时间:2017-10-26 17:03:14

标签: python pandas

我有像这样的pandas加载的contig文件:

    >NODE_1_length_4014_cov_1.97676
1       AATTAATGAAATAAAGCAAGAAGACAAGGTTAGACAAAAAAAAGAG...
2       CAAAGCCTCCAAGAAATATGGGACTATGTGAAAAGACCAAATCTAC...
3       CCTGAAAGTGACGGGGAGAATGGAACCAAGTTGGAAAACACTCTGC...
4       GAGAACTTCCCCAATCTAGCAAGGCAGGCCAACATTCAAATTCAGG...
5       CCACAAAGATACTCCTCGAGAAGAGCAACTCCAAGACACATAATTG...
6       GTTGAAATGAAGGAAAAAATGTTAAGGGCAGCCAGAGAGAAAGGTC...
7       GGGAAGCCCATCAGACTAACAGCGGATCTCTCGGCAGAAACCCTAC...
8       TGGGGGCCAATATTCAACATTCTTAAAGAAAAGAATTTTCAACCCA...
9       GCCAAACTAAGCTTCATAAGCAAAGGAGAAATAAAATCCTTTACAG...
10      AGAGATTTTGTCACCACCAGGCCTGCCTTACAAGAGCTCCTGAAGG...
11      GAAAGGAAAAACCGGTACCAGCCACTGCAAAATCATGCCAAACTGT...
12      CTAGGAAGAAACTGCATCAACTAATGAGCAAAATAACCAGCTAACA...
13      TCAAATTCACACATAACAATATTAACCTTAAATGTAAATGGGCTAA...
14      AGACACAGACTGGCAAATTGGATAAAGAGTCAAGACCCATCAGTGT...
15      ACCCATCTCAAATGCAGAGACACACATAGGCTCAAAATAAAGGGAT...
16      CAAGCAAATGGAAAACAAAAAAAGGCAGGGGTTGCAATCCTAGTCT...
17      TTTAAACCAACAAAGATCAAAAGAGACAAAGAAGGCCATTACATAA...
18      ATTCAACAAGAAGAGCTAACTATCCTAAATATATATGCACCCAATA...
19      TTCATAAAGCAAGTCCTCAGTGACCTACAAAGAGACTTAGACTCCC...
20      GGAGACTTTAACACCCCACTGTCAACATTAGACAGATCAACGAGAC...
21      GATATCCAGGAATTGAACTCAGCTCTGCACCAAGCGGACCTAATAG...
22      CTCCACCCCAAATCAACAGAATATACATTCTTTTCAGCACCACACC...
23      ATTGACCACATAGTTGGAAGTAAAGCTCTCCTCAGCAAATGTAAAA...
24      ACAAACTGTCTCTCAGACCACAGTGCAATCAAATTAGAACTCAGGA...
25      CAAAACTGCTCAACTACATGAAAACTGAACAACCTGCTCCTGAATG...
26      AACAAAATGAAGGCAGAAATAAAGATGTTCTTTGAAACCAATGAGA...
27      TACCAGAATCTCTGGGACGCATTCAAAGCAGTGTGTAGAGGGAAAT...
28      GCCCACAAGAGAAAGCAGGAAAGATCTAAAATTGACACCCTAACAT...
29      CTAGAGAAGCAAGAGCAAACACATTCAAAAGCTAGCAGAAGGCAAG...
                              ...                        
8540                         >NODE_2518_length_56_cov_219
8541    CCCTTGTTGGTGTTACAAAGCCCTTGAACTACATCAGCAAAGACAA...
8542                         >NODE_2519_length_56_cov_174
8543    CCGACTACTATCGAATTCCGCTCGACTACTATCGAATTCCGCTCGA...
8544                         >NODE_2520_length_56_cov_131
8545    CCCAGGAGACTTGTCTTTGCTGATGTAGTTCAAGAGCTTTGTAACA...
8546                         >NODE_2521_length_56_cov_118
8547    GGCTCCCTATCGGCTCGAATTCCGCTCGACTATTATCGAATTCCGC...
8548                          >NODE_2522_length_56_cov_96
8549    CCCGCCCCCAGGAGACTTGTCTTTGCTGATAGTAGTCGAGCGGAAT...
8550                          >NODE_2523_length_56_cov_74
8551    AGAGACTTGTCTTTGCTGATGTAGTTCAAGGGCTTTGTAACACCGA...
8552                          >NODE_2524_length_56_cov_70
8553    TGCTCGACTACTATCGAATTCCGCTCGACTACTATCGAATTCCGCT...
8554                          >NODE_2525_length_56_cov_59
8555    GAGACCCTTGTCGGTGTTACAAAGCCCTTTAACTACATCAGCAAAG...
8556                          >NODE_2526_length_56_cov_48
8557    CCGACTACTATCGAATTCCGCTCGACTACTATCGAATTCCGCTCGA...
8558                          >NODE_2527_length_56_cov_44
8559    CCAAGGGCTTTGTAACACCGACAAGGGTCTCGAAAACATCGGCATT...
8560                          >NODE_2528_length_56_cov_42
8561    GAGACCCTTGTAGGTGTTACAAAGCCCTTGAACTACATCAGCAAAG...
8562                          >NODE_2529_length_56_cov_38
8563    GAGACCCTTGTCGGTGTCACAAAGCCCTTGAACTACATCAGCAAAG...
8564                          >NODE_2530_length_56_cov_29
8565    GAGGGCTTTGTAACACCGACAAGGGTCTCGAAAACATCGGCATTCT...
8566                          >NODE_2531_length_56_cov_26
8567    AGGTTCAAGGGCTTTGTAACACCGACAAGGGTCTCGAAAACATCGG...
8568                          >NODE_2532_length_56_cov_25
8569    GAGATGTGTATAAGAGACTTGTCTTTGCTGATGTAGTTCAAGGGCT...

如何将这一列拆分为两列,在一列中生成> NODE _......并在另一列中生成相应的序列?另一个问题是序列是多行的,如何将它们组成一个字符串?结果预期如下:

    contig                                  sequence
    NODE_1_length_4014_cov_1.97676         AAAAAAAAAAAAAAA
    NODE_........                          TTTTTTTTTTTTTTT

非常感谢。

1 个答案:

答案 0 :(得分:0)

我无法重现您的示例,但我的猜测是您正在使用未以表格格式格式化的pandas加载文件。从您的示例看,您的文件格式化为:

>Identifier
    sequence
>Identifier
    sequence

在将信息放入pandas数据帧之前,您必须先解析文件。如果该行以'>节点'开头,则逻辑将循环遍历文件的每一行。您将该行存储为标识符。如果不是,则将它们连接到序列值。像这样:

testfile = '>NODE_1_length_4014_cov_1.97676\nAAAAAAAATTTTTTCCCCCCCGGGGGG\n>NODE_2518_length_56_cov_219\nAAAAAAAAGCCCTTTTT'.split('\n')
identifiers = []
sequences = []
current_sequence = ''
for line in testfile:
     if line.startswith('>'):
         identifiers.append(line)
         sequences.append(current_sequence)
         current_sequence = ''
     else:
         current_sequence += line.strip('\n')

df = pd.DataFrame({'identifiers' = identifiers, 
                   'sequences' = sequences})

此代码是否有效取决于您未提供的输入详细信息,但这可能会让您入门。