python - Pandas read_csv 列不正确

标签 python csv pandas

我正在尝试使用 pandas.read_csv 读取如下所示的数据文件(前 4 行)。

num NED IAU z N det TOF type alph.4 alph1 alph5 alph10 alph15 alph20 alph50  dSIbydnu  DWORRY UVNONE
1 ESO473-G007 J001605-234  0.06401 19.51 det  8.59 u  -0.432  -0.428  -0.413 -0.402 -0.395 -0.389 -0.369 0.017 53.53 UV
2 PKS0023-26 J0025-2602  0.32162 18.36 det  7.95 a  -0.272  -0.437  -0.726 -0.849 -0.919 -0.972 -1.135 -0.414 53.57 UV
3 NGC0315 0055+30  0.01648 18.84 det  7.41 a  -0.248  -0.306  -0.379 -0.398 -0.406 -0.411 -0.417 -0.119 53.60 UV

我输入 data = pandas.read_csv('radio.dat',sep=' ', header=0), 但是当我 print data 时,我得到 3 行标题,然后列名从它们应该开始的地方开始向下 3 列,我得到额外的 3 NaN 列:

                                           num      NED    IAU    z   N   det ...
1  ESO473-G007             J001605-234     NaN  0.06401  19.51  det NaN  8.59 ...
2  PKS0023-26              J0025-2602      NaN  0.32162  18.36  det NaN  7.95 ...
3  NGC0315                 0055+30         NaN  0.01648  18.84  det NaN  7.41 ...

num 应该是带有 1 2 3... 的列的标题,下一列是 NED,下一列是 IAU,NaN 甚至不应该在那里。

我试过设置 index_col=0 但这给了我这个错误:

  File "C:\Python27\lib\site-packages\pandas\io\parsers.py", line 1184, in read
    values = data.pop(self.index_col[i])
IndexError: list index out of range

设置 index_col=False 给出

  File "C:\Python27\lib\site-packages\pandas\io\parsers.py", line 1164, in read
    data = self._reader.read(nrows)
  File "pandas\parser.pyx", line 758, in pandas.parser.TextReader.read (pandas\parser.c:7411)
  File "pandas\parser.pyx", line 780, in pandas.parser.TextReader._read_low_memory (pandas\parser.c:7651)
  File "pandas\parser.pyx", line 855, in pandas.parser.TextReader._read_rows (pandas\parser.c:8484)
  File "pandas\parser.pyx", line 936, in pandas.parser.TextReader._convert_column_data (pandas\parser.c:9490)
  File "pandas\parser.pyx", line 1208, in pandas.parser.TextReader._get_column_name (pandas\parser.c:13172)
IndexError: list index out of range

如何正确读取此文件?

最佳答案

您的 csv 中有不同数量的空格,请使用 sep='\s+' 来处理此问题

In [128]:

t="""num NED IAU z N det TOF type alph.4 alph1 alph5 alph10 alph15 alph20 alph50  dSIbydnu  DWORRY UVNONE
1 ESO473-G007 J001605-234  0.06401 19.51 det  8.59 u  -0.432  -0.428  -0.413 -0.402 -0.395 -0.389 -0.369 0.017 53.53 UV
2 PKS0023-26 J0025-2602  0.32162 18.36 det  7.95 a  -0.272  -0.437  -0.726 -0.849 -0.919 -0.972 -1.135 -0.414 53.57 UV
3 NGC0315 0055+30  0.01648 18.84 det  7.41 a  -0.248  -0.306  -0.379 -0.398 -0.406 -0.411 -0.417 -0.119 53.60 UV"""
data = pd.read_csv(io.StringIO(t),sep='\s+', header=0)
data
​
Out[128]:
   num          NED          IAU        z      N  det   TOF type  alph.4  \
0    1  ESO473-G007  J001605-234  0.06401  19.51  det  8.59    u  -0.432   
1    2   PKS0023-26   J0025-2602  0.32162  18.36  det  7.95    a  -0.272   
2    3      NGC0315      0055+30  0.01648  18.84  det  7.41    a  -0.248   

   alph1  alph5  alph10  alph15  alph20  alph50  dSIbydnu  DWORRY UVNONE  
0 -0.428 -0.413  -0.402  -0.395  -0.389  -0.369     0.017   53.53     UV  
1 -0.437 -0.726  -0.849  -0.919  -0.972  -1.135    -0.414   53.57     UV  
2 -0.306 -0.379  -0.398  -0.406  -0.411  -0.417    -0.119   53.60     UV  

所以在这里:

alph50 dSIbydnu DWORRY

您在这些列之间以及您的数据之间也有双空格

关于python - Pandas read_csv 列不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29767544/

相关文章:

mongodb - 上传数据到 Meteor/Mongo DB

python - “NoneType”对象不可调用 Python Pandas Dataframe

python - 为给定的整数生成 x+xx+xxx+xxxx ...(对于 4 -> 4+44+444...)

python - 在线程中运行 Python 脚本并在 GUI 中将 stdout/stderr 重定向到 wx.TextCtrl

python - Pandas df.at() 引发 AttributeError : 'BlockManager' object has no attribute 'T'

Python - 仅读取并绘制最新文件

Python/Pandas - 将两列与 NaN 值合并

python - Merce csv 文件(来自文件夹)合并为一个,使用 Python 添加具有不同名称的列

python - 将 pandas 列中的字典转换为字典值

Python Pandas 使用文本文件创建 Dataframe