python - 如何将列表内的嵌套字典放入Dataframe中?

标签 python pandas

我正在尝试从交易所的 api 获取数据,我想它位于一个包含 dict 的列表中?所以我想创建一个表,其中包含名称、初始价格、价格、最高价、最低价等。问题:如何将它们转换为数据帧格式?带有 pd.Dataframe 的索引可以工作,但前提是它是一只股票。

pd.Series 是迄今为止我得到的最好的,如下所示:

1 {'BTC-ACM': {'initialprice': '0.00000390', 'pr...
0 {'BTC-AEON': {'initialprice': '0.00010617', 'p...

原始数据如下所示:

[{'BTC-ACM': {'initialprice': '0.00000380', 'price': '0.00000428', 'high': 
'0.00000510', 'low': '0.00000351', 'volume': '0.24647930', '
 bid': '0.00000433', 'ask': '0.00000465'}}, {'BTC-AEON': {'initialprice': 
'0.00010652', 'price': '0.00011040', 'high': '0.00013774', 'lo
 w': '0.00010616', 'volume': '1.17486173', 'bid': '0.00011040', 'ask': 
'0.00011867'}},....]

尝试将其设为这样的表格:

Name   | InitialPrice |Price |High |Low|
BTC-ACM| 0.0000132    |0.123 |0.9  |0.2|
BTC-AEO| 0.2131243    |0.213 |0.2  |0.1|

最佳答案

reshape 数据,然后调用DataFrame.to_dict:

df = pd.DataFrame.from_dict(
    {k : d[k] for d in data for k in d}, orient='index')

df[['initialprice', 'price', 'high', 'low']]

         initialprice       price        high         low
BTC-ACM    0.00000380  0.00000428  0.00000510  0.00000351
BTC-AEON   0.00010652  0.00011040  0.00013774  0.00010616

关于python - 如何将列表内的嵌套字典放入Dataframe中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52086012/

相关文章:

python - 与 Python 的代理连接

python - 数据框中的替换功能删除的内容超出预期

python - Pandas - 多指数均值

python - 如何检测特定对象 ID 的性别?

python - 在mysql加载数据文件导入期间删除CSV公式值

python - 有没有办法从 bash 运行 zip 文件中的 python 脚本?

Python滚动期返回

python - Pandas 在 true 时将 bool 列转换为列名

python - 通过多标签分类应用和绘制数据

python - 阻止机器人抓取我的 Google App Engine 网站