Python: Trying to shift data in CSV but not seeing desired results
Question: I'm having trouble with a relatively simple problem.. I have some data like so
in CSV:

    
    
    period, reading
    1, 3
    2, 4
    3, 5
    4, 2
    5, 2
    

I simply want the second column to shift up by one, while the first column
stays the same. After the second column shifts up by one, I would like the
last value to be replaced with a random value in the range -5, 5 (hopefully
you can see this from my script).

    
    
    import csv
    import random
    
    
    directory = r"C:/Program Files (x86)/CM/data.csv"
    
    with open(directory, 'r') as csvfile:
        s = csvfile.readlines()
    
    dataCSV = []
    
    for i, point in enumerate(s[1:]):
        seperatedPoint = point.strip("\n").split(",")
        if len(seperatedPoint) == 2:
            dataCSV.append([int(dataPoint) for dataPoint in seperatedPoint])
    
        l = len(dataCSV)
    
        for i in range(l-1):
            dataCSV[i][1] = dataCSV[i+1][1]
    
    dataCSV[l-1][1] += random.randint(-5,5)
    
    
    with open(directory, 'w') as csvfile: #opens the file for writing
        output = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)  
        output.writerow(["period", "reading"]) 
        header = (["period", "reading"])
        print( ", ".join( str(e) for e in header ) )
        print ""
        for point in dataCSV: 
            output.writerow(point) 
            print( ", ".join( str(e) for e in point ) )
            print ""
    

However instead of shifting up by one, this is just spitting out a the same
value a ton of times in the second column.. It will then change values and
spit out a ton of those repeated values as well, until I get to the end of my
range. I can't figure out what I'm doing wrong. Any help is appreciated, thank
you.


Answer: 
    import pandas as pd
    import random
    
    random.seed(time.time())
    df = pd.read_csv(filename)
    df[col] = df[col].shift(-1)
    df.iloc[-1][col] = random.randint(-5, 5)
    df.to_csv(outfile)
    

