read_table read all data to one column

前端 未结 1 1673
谎友^
谎友^ 2021-01-20 19:06

I got a file like this:

TCGA1 0 QWE
TCGA2 1 QWE
TCGA2 -2 RAF
TCGA3 2 KLS

and tried to read it into a dataframe using pandas.



        
相关标签:
1条回答
  • 2021-01-20 19:30

    You have to add sep='s\+' for separator arbitrary whitespace in read_table.

    It does not work as you expected, because default separator is ,, so all data are in one column.

    import pandas as pd
    import io
    
    temp=u"""TCGA1 0 QWE
    TCGA2 1 QWE
    TCGA2 -2 RAF
    TCGA3 2 KLS"""
    #after testing replace io.StringIO(temp) to filename
    df = pd.read_table(io.StringIO(temp), sep="\s+", header=None)
    print df
           0  1    2
    0  TCGA1  0  QWE
    1  TCGA2  1  QWE
    2  TCGA2 -2  RAF
    3  TCGA3  2  KLS
    

    Another solution with parameter delim_whitespace=True:

    import pandas as pd
    import io
    
    temp=u"""TCGA1 0 QWE
    TCGA2 1 QWE
    TCGA2 -2 RAF
    TCGA3 2 KLS"""
    #after testing replace io.StringIO(temp) to filename
    df = pd.read_table(io.StringIO(temp), delim_whitespace=True, header=None)
    print df
           0  1    2
    0  TCGA1  0  QWE
    1  TCGA2  1  QWE
    2  TCGA2 -2  RAF
    3  TCGA3  2  KLS
    
    0 讨论(0)
提交回复
热议问题