首页 > 解决方案 > How to convert a list of dictionary into a python panda dataframe?

问题描述

I am working with an API that will respond to me a list of dictionaries. the number of dictionaries in the list will vary but I need to convert the response into a pandas dataframe so that I can manipulate the data. Here is a sample of the response. How can I make a panda dataframe with headers (ticker, cap, when)

[{'ticker': 'KMG', 'cap_mm': 'NA', 'when': '--'}, {'ticker': 'SFIX', 'cap_mm': '2,665', 'when': 'amc'}]

标签: pythonpython-3.xpandas

解决方案


只需DataFrame像往常一样将其转换为pd.DataFrame(...)

>>> lod=[{'ticker': 'KMG', 'cap_mm': 'NA', 'when': '--'}, {'ticker': 'SFIX', 'cap_mm': '2,665', 'when': 'amc'}]
>>> pd.DataFrame(lod)
  cap_mm ticker when
0     NA    KMG   --
1  2,665   SFIX  amc
>>>

所以记得在问之前先尝试一下……


推荐阅读