How can I create a dictionary showing which entries in column a are associated with which entries in column b in a pandas dataframe?

Question:

I want to understand the relationships between the variables in columns A and B. They are string variables. For example

df = pd.DataFrame({'First_Name': ['Agatha', 'Agatha','Hercule', 'Hercule'],...                    
                   'Last Name': ['Christie', 'Raisin', 'Poirot', 'Holmes']})

I want some kind of data product that shows me:

Agatha: ['Christie', 'Raisin']
Hercule: ['Poirot', 'Holmes']

I would like to be able to do this without a loop.

Asked By: Liz

||

Answers:

df.groupby('First_Name',as_index=False)['Last Name'].agg(list)
    First_Name  Last Name
0   Agatha  [Christie, Raisin]
1   Hercule     [Poirot, Holmes]

with removing duplicates

df.drop_duplicates().groupby('First_Name',as_index=False)['Last Name'].agg(list)

Checking duplicates on First_Name and Last Name only


df.drop_duplicates(subset=['First_Name','Last Name']).groupby('First_Name',as_index=False)['Last Name'].agg(list)
Answered By: Naveed
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.