在python中加载具有单独列的文本文件

在python中加载具有单独列的文本文件,python,pandas,Python,Pandas,我有一个如下所示的文本文件: # Pearson correlation [n=344 #col=2] # Name Name Value BiasCorr 2.50% 97.50% N: 2.50% N:97.50% # --------------- --------------- -------- -------- -------- -------- -------- -------- 101_DGCA3.1D[0]

我有一个如下所示的文本文件:

# Pearson correlation [n=344 #col=2]
# Name             Name              Value   BiasCorr   2.50%   97.50%  N: 2.50% N:97.50%
# ---------------  ---------------  -------- -------- -------- -------- -------- --------
  101_DGCA3.1D[0]  101_LEC.1D[0]    +0.85189 +0.85071 +0.81783 +0.87777 +0.82001 +0.87849
我已使用以下代码将其加载到python pandas中:

import pandas as pd

data = pd.read_table('test.txt')
print data
但是,我似乎无法单独访问不同的列。我已尝试使用sep=''并复制文本文件中各列之间的空格,但仍然没有获得任何列名,并且尝试打印数据[0]时出现错误:

Traceback (most recent call last):
  File "cut_afni_output.py", line 3, in <module>
    print data[0]
  File "/home/user/anaconda2/lib/python2.7/site-packages/pandas/core/frame.py", line 1969, in __getitem__
    return self._getitem_column(key)
  File "/home/user/anaconda2/lib/python2.7/site-packages/pandas/core/frame.py", line 1976, in _getitem_column
    return self._get_item_cache(key)
  File "/home/user/anaconda2/lib/python2.7/site-packages/pandas/core/generic.py", line 1091, in _get_item_cache
    values = self._data.get(item)
  File "/home/user/anaconda2/lib/python2.7/site-packages/pandas/core/internals.py", line 3211, in get
    loc = self.items.get_loc(item)
  File "/home/user/anaconda2/lib/python2.7/site-packages/pandas/core/index.py", line 1759, in get_loc
    return self._engine.get_loc(key)
  File "pandas/index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas/index.c:3979)
  File "pandas/index.pyx", line 157, in pandas.index.IndexEngine.get_loc (pandas/index.c:3843)
  File "pandas/hashtable.pyx", line 668, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12265)
  File "pandas/hashtable.pyx", line 676, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12216)
KeyError: 0
我无法手动设置标题行,因为python似乎将整个内容视为一列。如何将文本文件作为我可以调用的单独列读取?

尝试以下方法:

In [33]: df = pd.read_csv(filename, comment='#', header=None, delim_whitespace=True)

In [34]: df
Out[34]:
                 0              1        2        3        4        5        6        7
0  101_DGCA3.1D[0]  101_LEC.1D[0]  0.85189  0.85071  0.81783  0.87777  0.82001  0.87849
试试这个:

In [33]: df = pd.read_csv(filename, comment='#', header=None, delim_whitespace=True)

In [34]: df
Out[34]:
                 0              1        2        3        4        5        6        7
0  101_DGCA3.1D[0]  101_LEC.1D[0]  0.85189  0.85071  0.81783  0.87777  0.82001  0.87849

您是否可以指定您收到的错误?已编辑以包含该错误,因为该错误太长,无法包含在注释中。您认为,对于试图获取不存在的名为0的列的数据[0],您应该得到什么。你想坐第一排吗?data.iloc[[0]]是。我只是不知道该怎么办,因为我没有列。你能指定你得到了哪个错误吗?编辑以包含错误,因为它太长了,无法包含在评论中。你认为你应该从数据[0]中得到什么?数据[0]试图获取一个名为0的列,但该列不存在。你想坐第一排吗?data.iloc[[0]]是。我只是不知道该怎么办,因为我没有专栏