'Add dict as value to dataframe

I want to add a dict to a dataframe and the appended dict has dicts or list as value.

Example:

abc = {'id': 'niceId', 
       'category': {'sport':'tennis', 
                    'land': 'USA'
                   },
        'date': '2022-04-12T23:33:21+02:00'
       }

Now, I want to add this dict to a dataframe. I tried this, but it failed:

df = pd.DataFrame(abc, columns = abc.keys())

Output:

ValueError: All arrays must be of the same length

I'm thankful for your help.



Solution 1:[1]

Your question is not very clear in terms of what your expected output is. But assuming you want to create a dataframe where the columns should be id, category, date and numbers (just added to show the list case) in which each cell in the category column keeps a dictionary and each cell in the numbers column keeps a list, you may use from_dict method with transpose:

abc = {'id': 'niceId', 
       'category': {'sport':'tennis', 
                    'land': 'USA'
                   },
        'date': '2022-04-12T23:33:21+02:00',
        'numbers': [1,2,3,4,5]
       }
    
df = pd.DataFrame.from_dict(abc, orient="index").T

gives you a dataframe as:

id category date numbers
0 niceId {'sport':'tennis','land': 'USA'} 2022-04-12T23:33:21+02:00 [1,2,3,4,5]

So let's say you want to add another item to this dataframe:

efg = {'id': 'notniceId', 
       'category': {'sport':'swimming', 
                    'land': 'UK'
                   },
        'date': '2021-04-12T23:33:21+02:00',
        'numbers': [4,5]
       }

df2 = pd.DataFrame.from_dict(efg, orient="index").T
pd.concat([df, df2], ignore_index=True)

gives you a dataframe as:

id category date numbers
0 niceId {'sport':'tennis','land': 'USA'} 2022-04-12T23:33:21+02:00 [1,2,3,4,5]
1 notniceId {'sport':'swimming','land': 'UK'} 2021-04-12T23:33:21+02:00 [4,5]

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 Beste