【问题标题】:Parse an incorrectly delimited text file using pandas使用 pandas 解析错误分隔的文本文件
【发布时间】:2016-12-22 19:23:29
【问题描述】:

我想将下面列出的文件解析为 pandas 数据框,其中年和月作为日期时间索引,其余 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

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

df = pd.read_csv(filename, delim_whitespace=True, index_col=[0], parse_dates=[[0, 1]], skiprows=17)

输出的一个sn-p:

             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_valueskwarg,但是没有效果。

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

【问题讨论】:

标签: python parsing pandas


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-19
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多