Python中扫描文件的替代方法,类似于R中的scan('file', what=list(...))是什么?

3

我可以帮忙翻译,以下是内容:

我有一个文件,格式如下:

10000 
2
2
2
2
0.00
0.00
0 1

0.00
0.01
0 1
...

我想从这个文件中创建一个DataFrame(跳过前5行),如下所示:
x1   x2    y1  y2
0.00 0.00  0   1
0.00 0.01  0   1

因此,这些行被转换为列(其中每三行也分成两列,y1和y2)。

在R中,我是这样做的:

df = as.data.frame(scan(".../test.txt", what=list(x1=0, x2=0, y1=0, y2=0), skip=5))

我正在寻找一种Python替代方案(例如Pandas),用于扫描(file, what=list(...))函数。是否存在该替代方案,还是我需要编写更复杂的脚本?

3个回答

3

您可以跳过前5个元素,然后取4个元素为一组构建Python列表,然后将其作为起点放入pandas...不过,如果pandas提供更好的解决方案也不会让我感到惊讶:

from itertools import islice, izip_longest

with open('input') as fin:
    # Skip header(s) at start
    after5 = islice(fin, 5, None)
    # Take remaining data and group it into groups of 4 lines each... The
    # first 2 are float data, the 3rd is two integers together, and the 4th
    # is the blank line between groups... We use izip_longest to ensure we
    # always have 4 items (padded with None if needs be)...
    for lines in izip_longest(*[iter(after5)] * 4):
            # Convert first two lines to float, and take 3rd line, split it and
            # convert to integers
        print map(float, lines[:2]) + map(int, lines[2].split())

#[0.0, 0.0, 0, 1]
#[0.0, 0.01, 0, 1]

谢谢Jon!如果pandas(或其他库)有一个类似于R中scan()这样更简洁的函数,那就太棒了。 - 2xu
1
+1 不错 @JonClements,你能解释一下吗? - Roman Pekar
@2xu 我不认为它会...但是有些人比我更有丰富的pandas经验...对于非平凡的预处理,你通常需要编写一个自定义函数来产生可用于DataFrame中的有效行... - Jon Clements
@RomanPekar 添加了一点内容 - 希望能帮到你 - 如果不行,请告诉我。 - Jon Clements
@JonClements 为什么需要在 after5 周围使用 iter - Roman Pekar
@RomanPekar 可能最好指向这个问题和答案 :) - Jon Clements

0
据我所知,我在这里http://pandas.pydata.org/pandas-docs/stable/io.html看不到任何选项来按照您的要求组织DataFrame;
但是您可以轻松实现它:
lines = open('YourDataFile.txt').read() # read the whole file
import re                               # import re
elems = re.split('\n| ', lines)[5:]     # split each element and exclude the first 5 
grouped = zip(*[iter(elems)]*4)          # group them 4 by 4
import pandas as pd                     # import pandas
df = pd.DataFrame(grouped)              # construct DataFrame
df.columns = ['x1', 'x2', 'y1', 'y2']   # columns names

它不够简洁,也不够优雅,但它清晰地表达了它的功能...


不错。得查一下 *iter(elems)*4 这一部分,但是找到了。 而且我并不追求优雅,只是采用了蛮力方法 :-) - 2xu
还有一个错别字(elem而不是elems)。很高兴你理解了;) - Giupo

0

好的,这是我是如何做到的(实际上是 Jon 和 Giupo 的答案的组合,谢谢你们!):

with open('myfile.txt') as file:
    data = file.read().split()[5:]
grouped = zip(*[iter(data)]*4)
import pandas as pd
df = pd.DataFrame(grouped)
df.columns = ['x1', 'x2', 'y1', 'y2']

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接