I have a list of dicts:
list_of_dicts = [{'name': 'a', 'counts': [{'dog': 2}]},
{'name': 'b', 'counts': [{'cat': 1}, {'capibara': 5}, {'whale': 10}]},
{'name': 'c', 'counts': [{'horse':1}, {'cat': 1}]]
I would like to transform this into a pandas dataframe like so:
| Name | Animal | Frequency |
|---|---|---|
| a | dog | 2 |
| b | cat | 1 |
| b | capibara | 5 |
| b | whale | 10 |
| c | horse | 1 |
| c | cat | 1 |
In the current code, I try to normalize it:
from pandas import json_normalize
df = json_normalize(list_of_dicts, 'counts')
But I think I am going in the wrong direction. Also, if I do a simple df = pd.DataFrame(list_of_dicts) , it results in each list of dicts being a single row value, which is not desired.