I have a dictionary that looks like this:
{'NorthernRegion': {'date': '2021-12-31',
'state': PA,
'candySales': 500,
'grocerySales': 1500,
'electronicSales': 800,
...
},
{'date': '2021-12-30',
...
},
{'date': '2021-12-29',
...
},
...
}
It is a nested dictionary. I want the nested dictionary to make up a Pandas data frame. For example, each row would be a date, and the other categories would make up the columns. What is the best way to go about this? I don't seem to be making any quality progress.
The data frame would look like
Date State Candy Sales Grocery Sales ...
-------------------------------------------------------
2021-12-31 PA 500 1500 ...
2021-12-30 PA 600 1600 ...
...
Each dictionary item needs a key and value, so assuming the complete structure of your dictionary looks something like the following:
sample_dict = {'NorthernRegion': {'date': '2021-12-31',
'state': 'PA',
'candySales': 500,
'grocerySales': 1500,
'electronicSales': 800},
'SouthernRegion':{'date': '2021-12-31',
'state': 'PA',
'candySales': 500,
'grocerySales': 1500,
'electronicSales': 800}}
To get the inner dictionaries as columns as the outer keys as index values, you can try pd.DataFrame(sample_dict).T
which will result in the following DataFrame:
date state candySales grocerySales electronicSales
NorthernRegion 2021-12-31 PA 500 1500 800
SouthernRegion 2021-12-31 PA 500 1500 800
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.