(Somewhat) new to python and coding in general! Using python 3.7 and pandas, I'm running code to create a searchable list of people in my dataframe, and I feel like my way of telling duplicates apart is very roundabout. I would love some advice on making it simpler and more efficient.
I have a database in pandas, 'df2', which has three relevant rows: 'First Name', 'Last Name' and 'People ID'. Some people will have the same name, and these can be told apart by their 'People ID'. I start by making the 'Name' column as thus:
df2['Name'] = df2['First Name'] + ' ' + df2['Last Name']
Now I make a Dict, nameid, to find out how many different People IDs are associated to each unique 'Name' string.
nameid = {}
for i in df2.index:
try:
nameid[df2.loc[i, 'Name']].append(df2.loc[i, 'People ID'])
except:
nameid[df2.loc[i, 'Name']] = [df2.loc[i, 'People ID']]
There are multiple occurences of each person in the spreadsheet, so I want to just have each unique instance of a different 'People ID' using set().
for i in nameid.keys():
nameid[i] = list(set(nameid[i]))
Now I create a second dict, namead, which is a "filtered" version of nameid where we've removed all reviewer names with just one ID value associated (those are fine as they are).
namead = {}
for i in nameid.keys():
paceholder = ['Nothing']
try:
paceholder.append(nameid[i][1])
namead[i] = nameid[i]
except:
pass
Finally, I use namead to make dupes, the list of index values of df2 where there are names belonging to different reviewers. I then pass that through df2 to add the 'People ID' to those names and ensure there is no confusion.
dupes = [i for i in df2.index if df2.loc[i, 'Name'] in namead.keys()]
for i in duperevs:
df2.loc[i, 'Name'] += ' ' + str(df2.loc[i, 'People ID'])
Whew! I feel like I added several layers of complexity here but I'm not sure where to start - help would be much appreciated!
EDIT - I'm not sure how to put an extract of the dataframe in this textbox. Clarification: each row of data has information, and I need it to be searchable by name for the end user, with a placeholder to differentiate identical names (the People ID). The resulting data looks like: "Frank Jones / 14498", "Mitin Nutil / 35589", "Maveh Kadini 1433 / 1433" (indicating that there's more than one Maveh Kadini in the data). Each person (by People ID) will appear in many different rows of data.