I'm a beginner with python. I'm trying to get the difference between two adjacent columns in a csv file using python 2.7.
Sample input:
Temperature 20 21 23 27 ...
Smoke Obscuration 0.1 0.3 0.6 0.7 ...
Carbon Dioxide 0.05 0.07 0.08 0.09 ...
......
......
I want to calculate the difference between two adjacent values and get the output like this:
Temperature 0 1 2 4 ...
Smoke Obscuration 0 0.2 0.3 0.1 ...
Carbon Dioxide 0 0.02 0.01 0.01 ...
......
......
this is as far as I got:
import csv
with open("test1.csv", "rb") as f_in, open("test2.csv", "w") as f_out:
r = csv.reader(f_in)
w = csv.writer(f_out)
for row in r:
for i, v in enumerate(row):
if i > 1:
v = (float(row[i]) - float(row[i-1]))
w.writerow(row)
It gave an error:
ValueError: could not convert string to float:
Could anyone help? Any guidance would be appreciated.