Reputation: 173
I have the following pandas dataframe:
df1 = pd.DataFrame({'date': [200101,200101,200101,200101,200102,200102,200102,200102],'blockcount': [1,1,2,2,1,1,2,2],'reactiontime': [350,400,200,250,100,300,450,400]})
I am trying to create a hierarchical dictionary, with the values of the embedded dictionary as lists, that looks like this:
{200101: {1:[350, 400], 2:[200, 250]}, 200102: {1:[100, 300], 2:[450, 400]}}
How would I do this? The closest I get is using this code:
df1.set_index('date').groupby(level='date').apply(lambda x: x.set_index('blockcount').squeeze().to_dict()).to_dict()
Which returns:
{200101: {1: 400, 2: 250}, 200102: {1: 300, 2: 400}}
Upvotes: 16
Views: 1626
Reputation: 323266
IIUC
df1.groupby(['date','blockcount']).reactiontime.agg(list).unstack(0).to_dict()
{200101: {1: [350, 400], 2: [200, 250]}, 200102: {1: [100, 300], 2: [450, 400]}}
Upvotes: 7
Reputation: 75080
Here is another way using pivot_table
:
d = df1.pivot_table(index='blockcount',columns='date',
values='reactiontime',aggfunc=list).to_dict()
print(d)
{200101: {1: [350, 400], 2: [200, 250]},
200102: {1: [100, 300], 2: [450, 400]}}
Upvotes: 22
Reputation: 11333
You can do the following,
df2 = df1.groupby(['date', 'blockcount']).agg(lambda x: pd.Series(x).tolist())
# Formatting the result to the correct format
dct = {}
for k, v in df2["reactiontime"].items():
if k[0] not in dct:
dct[k[0]] = {}
dct[k[0]].update({k[1]: v})
Which produces,
>>> {200101: {1: [350, 400], 2: [200, 250]}, 200102: {1: [100, 300], 2: [450, 400]}}
dct
holds the result you need.
Upvotes: 6