Karvy1
Karvy1

Reputation: 1075

Convert a Pandas DataFrame with repetitive keys to a dictionary

I have a DataFrame with two columns. I want to convert this DataFrame to a python dictionary. I want the elements of first column be keys and the elements of other columns in same row be values. However, entries in the first column are repeated -

Keys    Values
1       1
1       6
1       9
2       3
3       1
3       4

The dict I want is - {1: [1,6,9], 2: [3], 3: [1,4]}

I am using the code - mydict=df.set_index('Keys').T.to_dict('list') however, the output has only unique values of keys. {1: [9], 2: [3], 3: [4]}

Upvotes: 3

Views: 1929

Answers (2)

EdChum
EdChum

Reputation: 394159

IIUC you can groupby on the 'Keys' column and then apply list and call to_dict:

In[32]:
df.groupby('Keys')['Values'].apply(list).to_dict()

Out[32]: {1: [1, 6, 9], 2: [3], 3: [1, 4]}

Breaking down the above into steps:

In[35]:
# groupby on the 'Keys' and apply list to group values into a list
df.groupby('Keys')['Values'].apply(list)
Out[35]: 
Keys
1    [1, 6, 9]
2          [3]
3       [1, 4]
Name: Values, dtype: object

convert to a dict

In[37]:
# make a dict
df.groupby('Keys')['Values'].apply(list).to_dict()
Out[37]: {1: [1, 6, 9], 2: [3], 3: [1, 4]}

Thanks to @P.Tillman for the suggestion that to_frame was unnecessary, kudos to him

Upvotes: 4

Mohamed Thasin ah
Mohamed Thasin ah

Reputation: 11192

try this,

df.groupby('Keys')['Values'].unique().to_dict()

Output:

{1: array([1, 6, 9]), 2: array([3]), 3: array([1, 4])}

Upvotes: 1

Related Questions