I am having some trouble attempting to manipulate a CSV file and appending the results to a new column.
Essentially I have a csv file (delimited ;) with 5 columns currently (of Cartesian coords [X, Y] and components [dX, dY], and magnitude/ length). I wish to add the result of some equations, which differ depending on the value of my Cartesian components, to a 6th column in this csv file (the angle).
Thus far my code is this (the maths is correct [hopefully], it's just the appending that I'm having trouble with):
import csv, math
with open("mydata.csv", "rb") as f:
vectors = csv.reader(f, delimiter=";")
for col in vectors:
x = float(col[0])
y = float(col[1])
dX = float(col[2])
dY = float(col[3])
magnitude = float(col[4])
if dX > 0 and dY > 0:
comp = dY/dX
theta = math.degrees(math.atan(comp))
angle = 90 - theta
elif dX > 0 and dY < 0:
comp = dY/dX
theta = math.degrees(math.atan(comp))
angle = 90 + theta
elif dX < 0 and dY > 0:
comp = dX/dY
theta = math.degrees(math.atan(comp))
angle = 360 - theta
elif dX < 0 and dY < 0:
comp = dY/dX
theta = math.degrees(math.atan(comp))
angle = 270 - theta
So essentially, I want to add the angle
variable to a 6th column, for the correct line of my csv file.
I tried to create a new list and append (e.g.):
angles = []
...
angles.append(col)
angles.append(angle)
However, as you may have guessed I ended up with a line like this:
[[x, y, dX, dY, magnitude], angle]
Thanks for your help in advance.