0

I'm attempting to merge two datasets using a unique ID for clients that are present in only one dataset. I've assigned the unique IDs to each full name as a dictionary, but each person is unique, even if they have the same name. I need to assign each unique ID iteratively to each instance of that person's name.

example of the dictionary:

{'Corey Davis': {'names_id':[1472]}, 'Jose Hernandez': {'names_id': [3464,15202,82567,98472]}, ...}

I've already attempted using the .map() function as well as

referrals['names_id'] = referrals['full_name'].copy()
for key, val in m.items():
    referrals.loc[referrals.names_id == key, 'names_id'] = val

but of course, it only assigns the last value encountered, 98472.

I am hoping for something along the lines of:

full_name         names_id \
Corey Davis       1472
Jose Hernandez    3464
Jose Hernandez    15202
Jose Hernandez    82657
Jose Hernandez    98472

but I get

full_name         names_id \
Corey Davis       1472
Jose Hernandez    98472
Jose Hernandez    98472
Jose Hernandez    98472
Jose Hernandez    98472
chemmy
  • 25
  • 8

1 Answers1

0

Personally, what I would do is:

inputs = [{'full_name':'test', 'names_id':[1]}, {'full_name':'test2', 'names_id':[2,3,4]}]
# Create list of dictionaries for each 'entry'
entries = []
for input in inputs:
    for name_id in input['names_id']:
        entries.append({'full_name': input['full_name'], 'names_id': name_id})

# Now you have a list of dicts - each being one line of your table
# entries is now
# [{'full_name': 'test', 'names_id': 1},
# {'full_name': 'test2', 'names_id': 2},
# {'full_name': 'test2', 'names_id': 3},
# {'full_name': 'test2', 'names_id': 4}]

# I like pandas and use it for its dataframes, you can create a dataframe from list of dicts

import pandas as pd
final_dataframe = pd.DataFrame(entries)
Jeremy H
  • 408
  • 2
  • 11
  • I see what you're trying to do, but now I'm getting TypeError: string indices must be integers – chemmy Aug 08 '19 at 16:10
  • I'm guessing because the `inputs` I'm using is a list of dicts whereas you still have a dict? I did this because the dict you have won't work - you're overwriting `full_name` with whatever the latest `full_name` is. You can see what I mean by defining the output and the printing it – Jeremy H Aug 08 '19 at 16:26
  • I apologize, I created my dictionary differently and have edited. – chemmy Aug 08 '19 at 16:56