Reputation: 756
I would like to create a pandas data from a list of dicts and use the Keys as index For example:
x = [
{'AAPL': 0.07969655043031681},
{'MSFT': 0.04751221896383187},
{'NFLX': 0.009729232074671192}
]
I tried pd.DataFrame(x) but the keys are displayed as column and not as index
Could you help please . ?
Upvotes: 2
Views: 1866
Reputation: 88226
You can first create a single dictionary from the list of dictionaries using collections.ChainMap
, and then create a dataframe from it using DataFrame.from_dict
and specifying orient='index'
:
from collections import ChainMap
x = [{'AAPL': 0.07969655043031681}, {'MSFT': 0.04751221896383187},
{'NFLX': 0.009729232074671192}]
pd.DataFrame.from_dict(ChainMap(*x), orient='index', columns=['col1'])
col1
AAPL 0.079697
MSFT 0.047512
NFLX 0.009729
Upvotes: 5