Here is the python code that takes the data from my query and packages it to go into a csv file.
...
col_headers = [ i[0] for i in cursor.description ]
rows = [ list(i) for i in cursor.fetchall()]
df = pd.DataFrame(rows, columns=col_headers)
df.to_csv("PremiseCPE.csv", index=False)
for row in cursor.fetchall():
print (row)
...
The incoming data is in columns. I need to add an additional column (#6) called "Placemarks". I then need to add values in the new column row for each output from the database based on the values in in column #3 which is called cpeStatus. Below is the type of query structure I tried while creating a kml file:
...
iif (row[4]) = 'Off', (row[6]) = "http://maps.google.com/mapfiles/kml/shapes/forbidden.png"
ElseIf (row[4]) = 'Active', (row[6]) = "http://maps.google.com/mapfiles/kml/shapes/ranger_station.png"
ElseIf (row[4]) = 'Ready, (row[6]) = "http://maps.google.com/mapfiles/kml/shapes/mechanic.png"
ElseIf (row[4]) = 'Alarm', (row[6]) = "http://maps.google.com/mapfiles/kml/shapes/caution.png"
ElseIf (row[4]) = 'Null', (row[6]) = "http://maps.google.com/mapfiles/kml/shapes/white_bubble.png"
End If
...
The goal is to try to run this at the csv file level.
Can anyone help?
As @MattDMo says, you need to do this in the dataframe before writing the CSV. Also, I prefer a dictionary lookup to a long
if...elif...else
in python. Lastly, I suggest using pd.read_sql to query the database and create the df.yields the following df:
and the following CSV: