I have a dictionary like that:
JavaScript
x
3
1
dictionary = {'user1':[{'product1':10}, {'product2':15}, {'product3': 20}],
2
'user2':[{'product1':13}, {'product2':8}, {'product3': 50}]}
3
I want to construct a dataframe where I can see user1 and user2 as indices, product1, product2 and product3 as columns and values of these products should be values of columns. I tried looking here and found this post Construct pandas DataFrame from items in nested dictionary, but my format of data is different and I can’t find out how to make products to be my columns. So far I get “‘product1′:10” as my value in the first column of the first row. Using orient=’index’ made my main keys as indices, but that’s it. Please, help me.
Advertisement
Answer
One option would be to merge the lists
of dicts
into a single dict
then build a DataFrame.from_dict
:
JavaScript
1
11
11
1
import pandas as pd
2
from collections import ChainMap
3
4
dictionary = {'user1': [{'product1': 10}, {'product2': 15}, {'product3': 20}],
5
'user2': [{'product1': 13}, {'product2': 8}, {'product3': 50}]}
6
7
df = pd.DataFrame.from_dict(
8
{k: dict(ChainMap(*v)) for k, v in dictionary.items()},
9
orient='index'
10
)
11
df
:
JavaScript
1
4
1
product3 product2 product1
2
user1 20 15 10
3
user2 50 8 13
4
Optional alphanumeric sort with natsort
:
JavaScript
1
4
1
from natsort import natsorted
2
3
df = df.reindex(columns=natsorted(df.columns))
4
JavaScript
1
4
1
product1 product2 product3
2
user1 10 15 20
3
user2 13 8 50
4
JavaScript
1
2
1
{k: dict(ChainMap(*v)) for k, v in dictionary.items()}
2
JavaScript
1
3
1
{'user1': {'product3': 20, 'product2': 15, 'product1': 10},
2
'user2': {'product3': 50, 'product2': 8, 'product1': 13}}
3