使用pandas

时间:2016-12-22 19:23:30

标签: python parsing pandas

我想将下面列出的文件解析成一个pandas数据帧,其中年份和月份作为日期时间索引,其余11列列入dataframe列。

 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

我已经在文件中读到了文件名,使用下面的pandas命令:

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_values kwarg,但这没有效果。

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

1 个答案:

答案 0 :(得分:2)

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

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