I have this file in csv and I would like to create IDs by replacing the names with the IDs so they don't know who the people are. However I have this Name table below that as you can see has names that are repeated. I mean, where there are repeated names I substitute the same ID, and where there are different names I insert a new ID. I'd like to make it look like the IDs table I attached.
I think the idea is to take a record from the name column, read line by line comparing if there is a record equal to this one, if there is, repeat the id, if not, create a new id, adding these ids to a list and then adding this one list in a new column and removing the name column. I think this way, but I don't know how to do it How could I do this in Python? I have no idea
Table Name
Year,Name,Points,Sales Qtd,Sales date
2018,Mike,10,50,2018-01-01
2018,Joan,5,25,2018-01-02
2019,Joan,30,55,2019-01-05
2019,Micheal,10,35, 2019-03-15
2019,Micheal,5,20,2019-03-18
2019,Micheal,8,15,2019-04-20
2019,Elizabeth,20,50,2019-05-21
Tabe IDs
Year,NameID,Points,Sales qtd,sales date
2018,1,10,50,2018-01-01
2018,2,5,25,2018-01-02
2019,2,30,55,2019-01-05
2019,3,10,35,2019-03-15
2019,3,5,20,2019-03-18
2019,3,8,15,2019-04-20
2019,4,20,50,2019-05-21
I tried this code below with pandas but, it is not working
import pandas as pd
list_row = []
results = pd.read_csv('test_name.csv')
rowcount = 0
for row in open("test_name.csv"):
rowcount += 1
print("Number of lines present: ", rowcount)
first_row = results["Name"].loc[0]
print(first_row)
count = 0
count_reg = 0
count_row = 0
results = results.reset_index()
print(results["Name"].loc[count])
while count < rowcount:
print(results["Name"].loc[count], results["Name"].loc[count_reg])
for index, row in results.iterrows():
if results["Name"].loc[count] == results["Name"].loc[count_reg]:
count_reg += 1
list_row.append(count_row)
count_row += 1
else:
count_reg += 1
list_row.append(count_row)
count += 1
print("end")