Python中的CSV文件(滑动窗口)

时间:2016-09-08 09:54:43

标签: java python csv dataset pycharm

我是Python的初学者,我需要帮助操作Python中的csv文件。 我正在尝试为数据集中的每一行执行滑动窗口机制。

如果数据集是

,则为示例
timestamp | temperature | windspeed
965068200   9.61883  60.262   
965069100   9.47203  60.1664 
965070000   9.31125  60.0145   
965070900   9.13649  59.8064

如果用户指定的窗口大小为3,则结果应类似于

timestamp | temperature-2 | temperature-1 |temperature-0 | windspeed-2 | windspeed-1 | windspeed-0
965070000   9.61883 9.47203 9.31125 60.262 60.1664 60.0145
965070900   9.47203 9.31125 9.13649 60.1664 60.0145 59.8064

我可以通过在Java中使用List of ObjectsArray来实现这一点。阅读CSV并生成包含转换数据集的新CSV。 这是代码 http://pastebin.com/cQnTBg8d #researh

我需要在Python中执行此操作,请帮我解决此问题。

谢谢

1 个答案:

答案 0 :(得分:0)

这个答案假设你使用的是Python 3.x - 对于Python 2.x需要进行一些更改(一些明显的地方被评论)

对于问题中的数据格式,这可能是Python的起点:

import collections

def slide(infile,outfile,window_size):
    queue=collections.deque(maxlen=window_size)
    line=infile.readline()
    headers=[s.strip() for s in line.split("|")]
    row=[headers[0]]
    for h in headers[1:]
        for i in reversed(range(window_size)):
            row.append("%s-%i"%(h,i))
    outfile.write(" | ".join(row))
    outfile.write("\n")
    for line in infile:
        queue.append(line.split())
        if len(queue)==window_size:
            row=[queue[-1][0]]
            for j in range(1,len(headers)):
                for old in queue:
                    row.append(old[j])
            outfile.write("\t".join(row))
            outfile.write("\n")

ws=3
with open("infile.csv","r") as inf:
    with open("outfile.csv","w") as outf:
        slide(inf,outf,ws)

实际上这段代码都是关于使用队列来保存窗口的输入行而不是更多 - 其他一切都是文本到列表到文本。

使用实际的csv-data(参见注释)

import csv
import collections

def slide(infile,outfile,window_size):
    r=csv.reader(infile)
    w=csv.writer(outfile)
    queue=collections.deque(maxlen=window_size)
    headers=next(r) # r.next() on python 2
    l=[headers[0]]
    for h in headers[1:]
        for i in reversed(range(window_size)):
            l.append("%s-%i"%(h,i))
    w.writerow(l)
    hrange=range(1,len(headers))
    for row in r:
        queue.append(row)
        if len(queue)==window_size:
            l=[queue[-1][0]]
            for j in hrange:
                for old in queue:
                    l.append(old[j])
            w.writerow(l)

ws=3
with open("infile.csv","r") as inf: # rb and no newline param on python 2
    with open("outfile.csv","w") as outf: # wb and no newline param on python 2
        slide(inf,outf,ws)