python - 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'}]
解决方案
只需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
>>>
所以记得在问之前先尝试一下……
推荐阅读
- sql-server - 自动执行使用实体框架处理数据的 C# 代码?
- python - 从目录中的每个 .py 文件导入和测试变量
- python - 列表中每个值的出现次数是唯一的
- c# - 为什么基类方法不尊重 new 关键字?
- concurrency - .race 或 .hyper 何时优于非数据并行版本?
- python - 使用 Apply vs Manual 操作 Pandas 列
- c# - 如何通过查询参数值运行 ASP.NET MVC Web API 控制器方法?
- graph - 如何在 neo4j 中优化此查询?
- spring - 使用 Thymeleaf 访问会话范围的代理 bean
- php - 知道如何阻止段落遍历其 div 吗?