2016-05-12 15 views
2

にすべてのデータを読み取ります。read_tableは、私は、このようなファイルを持って1列

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

が、結果は次のように示した:

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

列名は逃してきた、そしてどのように私はそれを解決するだろうか? ありがとう!

答えて

1

セパレータの任意の空白にはsep='s\+'を追加する必要があります。これは、read_tableです。

デフォルトの区切り文字が,であるため、すべてのデータが1つの列になるため、期待通りに機能しません。パラメータ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), 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 

別の解決策:

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

あなたの助けをありがとう!私は以前これを無視しました。 –

関連する問題