Aoi Summer
Aoi Summer

Reputation: 35

read_table read all data to one column

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.

In [16]: df = pd.read_table('TR.txt',header=None)

but the result showed like this:

In [17]: df
Out[17]: 
          0
0   TCGA1 0 QWE
1   TCGA2 1 QWE
2  TCGA2 -2 RAF
3   TCGA3 2 KLS

Column names has been missed, and how could I solve that? Thanks!

Upvotes: 3

Views: 1299

Answers (1)

jezrael
jezrael

Reputation: 863166

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

Upvotes: 2

Related Questions