CSV Files in Python (Sliding Window)
<p>I am a beginner to Python, I need a help with manipulating csv files in Python.
I am trying to do sliding window mechanism for each row in dataset.</p>

<p>for an example if the dataset is this</p>

<pre><code>timestamp | temperature | windspeed
965068200   9.61883  60.262   
965069100   9.47203  60.1664 
965070000   9.31125  60.0145   
965070900   9.13649  59.8064
</code></pre>

<p>and if user specified window size is 3,the result should be something like </p>

<pre><code>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
</code></pre>

<p>I could do this by using List of ObjectsArray in Java.Reading CSV and generate new CSV which it contains transformed dataset.
Here is the code 
<a href="http://pastebin.com/cQnTBg8d" rel="nofollow">http://pastebin.com/cQnTBg8d</a> #researh</p>

<p>I need to do this in Python , please help me to solve this.</p>

<p><em>Thank you</em></p>

<p><strong>This answer assumes you are using Python 3.x - for Python 2.x some changes are required (some obvious places are commented)</strong></p>

<p>For the data format in the question, this could be a starting point in Python:</p>

<pre><code>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)
</code></pre>

<p>actually this code is all about using a queue to keep the input rows for the window and not more - everything else is text-to-list-to-text.</p>

<p>With actual csv-data (see comment)</p>

<pre><code>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)
</code></pre>

java python csv dataset pycharm