Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用pandas分析分隔错误的文本文件_Python_Parsing_Pandas - Fatal编程技术网

Python 使用pandas分析分隔错误的文本文件

Python 使用pandas分析分隔错误的文本文件,python,parsing,pandas,Python,Parsing,Pandas,我想将下面列出的文件解析为一个pandas数据框架,其中year和month作为日期时间索引,其余11列解析为数据框架列 STANDARDIZED NORTHERN HEMISPHERE TELECONNECTION INDICES (1981-2010 Clim) column 1: Year (yy) column 2: Month (mm) column 3: North Atlantic Oscillation (NAO) column 4: East Atlantic Patter

我想将下面列出的文件解析为一个pandas数据框架,其中year和month作为日期时间索引,其余11列解析为数据框架列

 STANDARDIZED NORTHERN HEMISPHERE TELECONNECTION INDICES (1981-2010 Clim)

column 1: Year (yy)
column 2: Month (mm)
column 3: North Atlantic Oscillation (NAO)
column 4: East Atlantic Pattern (EA)
column 5: West Pacific Pattern (WP)
column 6: EastPacific/ North Pacific Pattern (EP/NP)
column 7: Pacific/ North American Pattern (PNA)
column 8: East Atlantic/West Russia Pattern (EA/WR)
column 9: Scandinavia Pattern (SCA)
column 10: Tropical/ Northern Hemisphere Pattern (TNH)
column 11: Polar/ Eurasia Pattern (POL)
column 12: Pacific Transition Pattern (PT)
column 13: Explained Variance (%) of leading 10 modes
PATTERN VALUES ARE SET TO -99.9 FOR MONTHS IN WHICH THE PATTERN IS NOT A LEADING MODE

yyyy mm   NAO   EA    WP   EP/NP  PNA  EA/WR  SCA   TNH   POL  PT    Expl. Var.

1950  1   0.56 -2.71 -1.69  0.91 -3.65  2.29  0.78  0.55 -0.71-99.90   86.0
1950  2   0.01  0.66 -1.36 -1.13 -1.69 -0.57 -0.94 -1.07  1.25-99.90   58.6
1950  3  -0.78  0.82 -0.38 -0.02 -0.06 -1.80 -0.22-99.90  0.78-99.90   54.3
1950  4   0.65  0.28 -0.50 -1.87 -0.23 -2.50  0.46-99.90  0.10-99.90   64.8
1950  5  -0.50 -0.51  0.23 -0.98 -0.40  1.41  0.28-99.90  0.55-99.90   49.6
我已使用以下命令读取文件名:

df = pd.read_csv(filename, delim_whitespace=True, index_col=[0], parse_dates=[[0, 1]], skiprows=17)
输出的一个片段:

             NAO    EA           WP  EP/NP   PNA  EA/WR          SCA  \
yyyy_mm                                                                
1950-01-01  0.56 -2.71        -1.69   0.91 -3.65   2.29         0.78   
1950-02-01  0.01  0.66        -1.36  -1.13 -1.69  -0.57        -0.94   
1950-03-01 -0.78  0.82        -0.38  -0.02 -0.06  -1.80  -0.22-99.90   
1950-04-01  0.65  0.28        -0.50  -1.87 -0.23  -2.50   0.46-99.90   
1950-05-01 -0.50 -0.51         0.23  -0.98 -0.40   1.41   0.28-99.90 
虽然我能够正确地解析大部分数据,但
-99.90
数据值似乎没有与前一个值分隔开来,因此被集中到前一列中。我假设这些值无论如何都会被标记,所以我很乐意从结果数据帧中省略它们

我使用了
na_值
kwarg,但没有效果


如果这个问题有一个内置的解决方案,或者我需要在解析之前编写一个定制的文本解析器吗?如果需要自定义解析器,在解析之前消除/替换
-99.90
值的最直接方法是什么,以便正确解析生成的数据帧?

手动读取标题并指定宽度:

with open(filename) as fobj:
    for _ in range(17):
        fobj.readline()
    names = fobj.readline().split()
    names = names[:-2] + [' '.join(names[-2:]) ]
    fobj.readline()
    widths = [4, 3, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6]
    df = pd.read_fwf(fobj, widths=widths, names=names, index_col=False)
结果:

   yyyy  mm   NAO    EA    WP  EP/NP   PNA  EA/WR   SCA    TNH   POL    PT  Expl. Var.
0  1950   1  0.56 -2.71 -1.69   0.91 -3.65   2.29  0.78   0.55 -0.71 -99.9       86.0
1  1950   2  0.01  0.66 -1.36  -1.13 -1.69  -0.57 -0.94  -1.07  1.25 -99.9       58.0
2  1950   3 -0.78  0.82 -0.38  -0.02 -0.06  -1.80 -0.22 -99.90  0.78 -99.9       54.0
3  1950   4  0.65  0.28 -0.50  -1.87 -0.23  -2.50  0.46 -99.90  0.10 -99.9       64.0
4  1950   5 -0.50 -0.51  0.23  -0.98 -0.40   1.41  0.28 -99.90  0.55 -99.9       49.0
尝试使用而不是
read\u csv()