Skip to content Skip to sidebar Skip to footer

Converting Pandas DF Into Special Dictionary?

I have a DataFrame with customer_id, date, product_id that they bought. I want to convert this DataFrame to 2 dictionary customer_id date product_id 1 10/3/2017

Solution 1:

There is problem with duplicates per group in first and third row, so was returned 1: 7384. Solution use groupby with lambda function for convert to dict per group and then to_dict for convert final Series:

d = df.groupby('date').apply(lambda x: dict(zip(x['customer_id'], x['product_id']))).to_dict()
print (d)
{'10/3/2017': {1: 7384, 2: 1234}, '11/3/2017': {2: 4321}}

EDIT:

s = df.groupby(['date','customer_id'])['product_id'].apply(list)
d = {k: v[k].to_dict() for k, v in s.groupby(level=0)}
print (d)
{'10/3/2017': {1: [1234, 7384], 2: [1234]}, '11/3/2017': {2: [4321]}}

And if dont need one item lists:

s = (df.groupby(['date','customer_id'])['product_id']
       .apply(lambda x: list(x) if len(x) > 1 else x.iat[0]))
d = {k: v[k].to_dict() for k, v in s.groupby(level=0)}
print (d)
{'10/3/2017': {1: [1234, 7384], 2: 1234}, '11/3/2017': {2: 4321}}

Solution 2:

Adding on the solution by jezrael, you could use list inside the apply function instead of dict to get the output you need. That way duplicates won't be deleted as we see by using dict.

d = df.groupby('date').apply(lambda x: list((zip(x['customer_id'], x['product_id'])))).to_dict()

The output is :

{'10/3/2017': [(1, 1234), (1, 7384), (2, 1234)], '11/3/2017': [(2, 4321)]}

Post a Comment for "Converting Pandas DF Into Special Dictionary?"