【问题标题】:Can pandas handle variable-length whitespace as column delimiters [duplicate]熊猫可以将可变长度空格作为列分隔符处理吗?
【发布时间】:2012-08-14 20:13:51
【问题描述】:

我有一个文本文件,其中的列由可变数量的空格分隔。是否可以在不预处理文件的情况下直接将此文件作为熊猫数据框加载?在pandas documentation the delimiter section 中说我可以使用's*' 构造,但我无法让它工作。

## sample data
head sample.txt

#                                                                            --- full sequence --- -------------- this domain -------------   hmm coord   ali coord   env coord
# target name        accession   tlen query name           accession   qlen   E-value  score  bias   #  of  c-Evalue  i-Evalue  score  bias  from    to  from    to  from    to  acc description of target
#------------------- ---------- ----- -------------------- ---------- ----- --------- ------ ----- --- --- --------- --------- ------ ----- ----- ----- ----- ----- ----- ----- ---- ---------------------
ABC_membrane         PF00664.18   275 AAF67494.2_AF170880  -            615     8e-29  100.7  11.4   1   1     3e-32     1e-28  100.4   7.9     3   273    42   313    40   315 0.95 ABC transporter transmembrane region
ABC_tran             PF00005.22   118 AAF67494.2_AF170880  -            615   2.6e-20   72.8   0.0   1   1   1.9e-23   6.4e-20   71.5   0.0     1   118   402   527   402   527 0.93 ABC transporter
SMC_N                PF02463.14   220 AAF67494.2_AF170880  -            615   3.8e-08   32.7   0.2   1   2    0.0036        12    4.9   0.0    27    40   391   404   383   408 0.86 RecF/RecN/SMC N terminal domain
SMC_N                PF02463.14   220 AAF67494.2_AF170880  -            615   3.8e-08   32.7   0.2   2   2   1.8e-09   6.1e-06   25.4   0.0   116   210   461   568   428   575 0.85 RecF/RecN/SMC N terminal domain
AAA_16               PF13191.1    166 AAF67494.2_AF170880  -            615   3.1e-06   27.5   0.3   1   1     2e-09     7e-06   26.4   0.2    20   158   386   544   376   556 0.72 AAA ATPase domain
YceG                 PF02618.11   297 AAF67495.1_AF170880  -            284   3.4e-64  216.6   0.0   1   1   2.9e-68     4e-64  216.3   0.0    68   296    53   274    29   275 0.85 YceG-like family
Pyr_redox_3          PF13738.1    203 AAF67496.2_AF170880  -            352   2.9e-28   99.1   0.0   1   2   2.8e-30   4.8e-27   95.2   0.0     1   201     4   198     4   200 0.85 Pyridine nucleotide-disulphide oxidoreductase

#load data
from pandas import *
data = read_table('sample.txt', skiprows=3, header=None, sep=" ")

ValueError: Expecting 83 columns, got 91 in row 4

#load data part 2
data = read_table('sample.txt', skiprows=3, header=None, sep="'s*' ")
#this mushes some of the columns into the first column and drops the rest.
    X.1
1    ABC_tran PF00005.22 118 AAF67494.2_
2    SMC_N PF02463.14 220 AAF67494.2_
3    SMC_N PF02463.14 220 AAF67494.2_
4    AAA_16 PF13191.1 166 AAF67494.2_
5    YceG PF02618.11 297 AAF67495.1_
6    Pyr_redox_3 PF13738.1 203 AAF67496.2_
7    Pyr_redox_3 PF13738.1 203 AAF67496.2_
8    FMO-like PF00743.14 532 AAF67496.2_
9    FMO-like PF00743.14 532 AAF67496.2_

虽然我可以预处理文件以将空格更改为逗号/制表符,但直接加载它们会很好。

(仅供参考,这是来自 hmmscan program 的 *.hmmdomtblout 输出)

【问题讨论】:

  • 我觉得熊猫除了嚼竹子和打喷嚏什么都做不了
  • 吃....芽...叶子..抓住我。
  • @zach:我认为问题在于最后一列中的值包含分隔符。
  • @MRAB。很好 - 我认为那是完全正确的。看起来像是不可避免的预处理 - 我必须按宽度解析列。
  • @zach:如果您有后续问题,请为其打开一个新问题。否则,解决问题变得非常困难。

标签: python pandas


【解决方案1】:

在这种情况下,给出的答案都不起作用:

Block..Col.name.with.spaces..col3
...1..6.141754e+003..2.998903e+000
2048..6.154461e+003..6.010216e+000

即两个或多个空格用作分隔符,但列名本身可以包含一个空格。

在这种情况下,我们需要两个或多个空格的正则表达式。这将起作用:

sep=r"[ ]{2,}"

但同样,缺点是它会触发 python 解析器。

【讨论】:

    【解决方案2】:

    你应该能够做到这一点,@DSM 刚刚在另一个帖子中教过我:

    data = read_table('sample.txt', skiprows=3, header=None, delim_whitespace=True)
    

    Documentation

    【讨论】:

    • 带有正则表达式分隔符的解析器无法使用C引擎,运行时我得到ParserWarning。显然delim_whitespace=True的解决方案使用了C引擎,所以应该更快
    【解决方案3】:

    我认为文档中缺少\(可能是因为它在某些时候被解释为转义标记?)毕竟这是一个正则表达式:

    In [68]: data = read_table('sample.txt', skiprows=3, header=None, sep=r"\s*")
    
    In [69]: data
    Out[69]: 
    <class 'pandas.core.frame.DataFrame'>
    Int64Index: 7 entries, 0 to 6
    Data columns:
    X.1     7  non-null values
    X.2     7  non-null values
    X.3     7  non-null values
    X.4     7  non-null values
    X.5     7  non-null values
    X.6     7  non-null values
    [...]
    X.23    7  non-null values
    X.24    7  non-null values
    X.25    5  non-null values
    X.26    3  non-null values
    dtypes: float64(8), int64(10), object(8)
    

    由于@MRAB提到的分隔符问题,最后几列有些问题:

    In [73]: data.ix[:,20:]
    Out[73]: 
       X.21  X.22           X.23                   X.24            X.25    X.26
    0   315  0.95            ABC            transporter   transmembrane  region
    1   527  0.93            ABC            transporter            None    None
    2   408  0.86  RecF/RecN/SMC                      N        terminal  domain
    3   575  0.85  RecF/RecN/SMC                      N        terminal  domain
    4   556  0.72            AAA                 ATPase          domain    None
    5   275  0.85      YceG-like                 family            None    None
    6   200  0.85       Pyridine  nucleotide-disulphide  oxidoreductase    None
    

    但最后可以修补。

    【讨论】:

    • 如何修补?
    • 添加 engine='python' 可避免此解决方案给出的其他警告
    • 正确的分隔符不应该是sep=r"\s+"吗?我们希望至少有一个空间。
    猜你喜欢
    • 2019-05-17
    • 2020-04-09
    • 2020-03-11
    • 1970-01-01
    • 1970-01-01
    • 2016-11-27
    相关资源
    最近更新 更多