I want to make a python script that subtracts the last value with the previous value and stores the result in a new column in a new text file

I have a column for some data points from 1 to 19999 in a text file, I want to subtract each new value with the previous value and save the response in a new column in a new text file.

My data points (in a column) with the index number in a text file are as follows:

1 66.295962
2 66.318076
3 66.479436
4 66.460284
5 66.551778
6 66.303606
7 66.222943
8 66.50905
9 66.268011
10 66.291807
.    .
.    .
.    .
.    .
19993 69.303592
19994 69.384204
19995 69.375126
19996 69.417533
19997 69.296388
19998 69.284336
19999 69.403861

How to make a program for this in python, any help would be really appreciated. Thanks in advance, I already tried to convert my column to a list, but I cannot figure out how to subtract the values ​​and save the answer in a new column of a new text file ....

My program looks like this:

f = open ('infilename', 'r')
for line in f:
    line = line.strip()
    columns = line.split()
   # for i in xrange(len(columns)):
    print columns[1]

import csv

#open file
infile = open('infilename', 'r')

#define csv reader object, assuming delimiter is tab
tsvfile = csv.reader(infile, delimiter='\t')

lines = []

## iterate through lines in file
for line in tsvfile:
    lines.append(line)

print "Col1",[line[0] for line in lines]
+4
source share
3

noob, Python NumPy . NumPy ( Pandas), . ,

( , ):

1 66.295962   
2 66.318076
3 66.479436
4 66.460284
5 66.551778
6 66.303606
7 66.222943
8 66.50905
9 66.268011
10 66.291807

,

>>> import numpy as np
>>> import numpy.lib.recfunctions as rfn

(dtype).

>>> f = np.loadtxt("f:/test/test.txt",dtype=[('ID','int32'),('Value','float64')])
>>> f
array([(1, 66.295962), (2, 66.318076), (3, 66.479436), (4, 66.460284),
       (5, 66.551778), (6, 66.303606), (7, 66.222943), (8, 66.50905),
       (9, 66.268011), (10, 66.291807)], 
      dtype=[('ID', '<i4'), ('Value', '<f8')])

, / ,

>>> a = f['Value']
>>> a1 = np.roll(a,-1)
>>> diff = a1-a

, ( mumbo-jumbo, , )

>>> out = rfn.merge_arrays((f, diff), asrecarray=True, flatten=True)

, , .

>>> np.savetxt("f:/test/out.txt", out, delimiter="",fmt='% 10i % 10.6f % 10.6f')

( ..).

     1  66.295962   0.022114
     2  66.318076   0.161360
     3  66.479436  -0.019152
     4  66.460284   0.091494
     5  66.551778  -0.248172
     6  66.303606  -0.080663
     7  66.222943   0.286107
     8  66.509050  -0.241039
     9  66.268011   0.023796
    10  66.291807   0.004155

... - . , , , . , 2. , / 0, , .

+1

:

input_list = [(i, float(num.split()[1])) for i, num in enumerate(f.read().splitlines())]
output_list = [input_list[i[0]][1] - input_list[i[0]-1][1] for i in input_list if i[0] != 0]

:

0.022114
0.16136
-0.019152
0.091494
-0.248172
-0.080663
0.286107
-0.241039
0.023796
+1

CSV , itertools.izip , :

import csv
from itertools import izip_longest
from operator import sub
with open('new.txt', 'r') as csvfile,open('out.txt','w') as out:
     spamreader = csv.reader(csvfile, delimiter=' ')
     z=izip_longest(*spamreader)
     next(z)
     z=next(z)
     try :
        for i,j in izip_longest(z,z[1:]):
           out.write(str(sub(float(j),float(i)))+'\n')
     except:
        pass

Note that the izip_longestgenerator containing your columns returns, and you can access the elements of the generator by the method next, in this case we do not need the first column, which is the identifier, so after the call, nextthen put the result of another method next, which is a column of numbers. and then apply the function again izip_longestto this column to get the expected pairs.

Also note that spamreaderthis is a csv reader object that returns a generator containing all the lines.

Demo:

#your file

1 66.295962
2 66.318076
3 66.479436
4 66.460284
5 66.551778
6 66.303606
7 66.222943
8 66.50905
9 66.268011
10 66.291807

#output

0.022114
0.16136
-0.019152
0.091494
-0.248172
-0.080663
0.286107
-0.241039
0.023796
+1
source

All Articles