I would like to add a new column to my CSV in Python. I understand that in Python I will have to run the CSV and rewrite a new one as an output to add a column.
I want to run though each UserID and assign a UniqueID to it.
This is my input:
UserID,name
a,alice
a,alice
b,ben
c,calvin
c,calvin
c,calvin
This is my desired output:
UniqueID,UserID,name
1,a,alice
1,a,alice
2,b,ben
3,c,calvin
3,c,calvin
3,c,calvin
I am new to Python and was wondering if anyone can show me how this can be done. Thanks.
Here is my code so far:
import csv
import operator
temp_index = 0
with open("./coordinates.csv") as all_coordinates_csv:
coordinate_reader = csv.reader(all_coordinates_csv, delimiter=",")
sort = sorted(coordinate_reader,key=operator.itemgetter(0))
with open("./sorteduserid.csv","wb") as sorteduser_csv:
csv_writer = csv.writer(sorteduser_csv,delimiter=",")
csv_writer.writerows(sort)