将我的字典变成熊猫数据框 - python

我有一个函数,可以根据某些条件创建多个命令。

但是,我真的很想在收集字典后将其转换为数据框。
但是我找不到一个简单的方法...现在,我在想解决方案是将字典中的每个键乘以最内部字典中键的数量,但是希望有更好的方法

由于我的函数创建了字典,因此,如果有更好的方法,则可以以任何方式对其进行更改。

这是我的字典

{'TSLA': {2011: {'negative': {'lowPrice': 185.16,
    'lowDate': '05/27/19',
    'highPrice': 365.71,
    'highDate': '12/10/18',
    'change': -0.49}},
  2012: {'negative': {'lowPrice': 185.16,
    'lowDate': '05/27/19',
    'highPrice': 365.71,
    'highDate': '12/10/18',
    'change': -0.49}},
  2013: {'negative': {'lowPrice': 32.91,
    'lowDate': '01/07/13',
    'highPrice': 37.24,
    'highDate': '03/26/12',
    'change': -0.12},
   'positive': {'lowPrice': 32.91,
    'lowDate': '01/07/13',
    'highPrice': 190.9,
    'highDate': '09/23/13',
    'change': 4.8}}}}

我期望的输出将是这样的,当然带有值:

                    lowPrice lowDate highPrice highDate change
ATVI  2012 Negative      NaN     NaN       NaN      NaN  NaN
           Positive      NaN     NaN       NaN      NaN  NaN
      2013 Negative      NaN     NaN       NaN      NaN  NaN
TSLA  2014 Positive      NaN     NaN       NaN      NaN  NaN
      2012 Negative      NaN     NaN       NaN      NaN  NaN
      2013 Positive      NaN     NaN       NaN      NaN  NaN
      2014 Positive      NaN     NaN       NaN      NaN  NaN

参考方案

您可以将嵌套字典平整两次以获取键的元组,然后传递给DataFrame.from_dict

d1 = {(k1, k2, k3): v3 
      for k1, v1 in d.items() 
      for k2, v2 in v1.items()
      for k3, v3 in v2.items()}

df = pd.DataFrame.from_dict(d1, orient='index')
#alternative
#df = pd.DataFrame(d1).T
print (df)
                   lowPrice   lowDate highPrice  highDate change
TSLA 2011 negative   185.16  05/27/19    365.71  12/10/18  -0.49
     2012 negative   185.16  05/27/19    365.71  12/10/18  -0.49
     2013 negative    32.91  01/07/13     37.24  03/26/12  -0.12
          positive    32.91  01/07/13     190.9  09/23/13    4.8

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

Python-Excel导出 - python

我有以下代码:import pandas as pd import requests from bs4 import BeautifulSoup res = requests.get("https://www.bankier.pl/gielda/notowania/akcje") soup = BeautifulSoup(res.cont…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

Python:如何根据另一列元素明智地查找一列中的空单元格计数? - python

df = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice','Jane', 'Alice','Bob', 'Alice'], 'income…