'How do I convert a large list of dictionaries into a pandas DataFrame?

I would like to create a dataframe from 9 million + dictionary records:

defaultdict(<class 'dict'>, {'movie1': {'user1': '2', 'user2': '4'},
                             'movie2': {'user2': '5'},
                             'movie3': {'user1': '2'}})

I've attempted using pd.DataFrame() which doesn't work due to the size.

Output I would like:

dataframe with user1, user2 as index, movie... as columns



Solution 1:[1]

Simply feed it to the DataFrame constructor:

df = pd.DataFrame(d)

output:

      movie1 movie2 movie3
user1      2    NaN      2
user2      4      5    NaN

used input:

from collections import defaultdict

d = defaultdict(dict, {'movie1': {'user1': '2', 'user2': '4'},
                       'movie2': {'user2': '5'},
                       'movie3': {'user1': '2'}})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mozway