将字典嵌套到数据框中

时间:2020-08-06 15:15:24

标签: python pandas dataframe dictionary

嗨,我有一个类似下面的python字典数据结构

defaultdict(<function __main__.<lambda>()>,
            {'AMZN': [{'improv_sharpe': -4.9719673781114265e-05},
              {'stan_sharpe': 0.5995152805688786},
              {'prob_sharpe': 1.0159440451398998e-19},
              {},
              {},
              {}],
             'GOOGL': [{'improv_sharpe': 2.6114809517957284e-05},
              {'stan_sharpe': 0.16322865760123956},
              {'prob_sharpe': 6.376598832386561e-72},
              {},
              {},
              {}]})

如何将以上内容转换为以下数据框:

                      AMZN                                     GOOGL
improv_sharpe': -4.9719673781114265e-05                   2.6114809517957284e-05

1 个答案:

答案 0 :(得分:1)

这可以通过列表和字典理解来解决,就像许多类似的问题一样,但是我找不到确切的重复项。

如果您的示例数据是:

example_data = {'AMZN': [{'improv_sharpe': -4.9719673781114265e-05},
              {'stan_sharpe': 0.5995152805688786},
              {'prob_sharpe': 1.0159440451398998e-19},
              {},
              {},
              {}],
             'GOOGL': [{'improv_sharpe': 2.6114809517957284e-05},
              {'stan_sharpe': 0.16322865760123956},
              {'prob_sharpe': 6.376598832386561e-72},
              {},
              {},
              {}]
 }

然后:

import pandas as pd
pd.DataFrame({comp_nm :     # company name is the column name
               pd.Series({  # company data is the column vector
                  k:v for d in comp_data # since your company entries are lists of length one 
                      for k,v in d.items() if d # remove empty {}'s which will throw error
                 }) 
              # each company is a dict with name as key and list of length one as value
              for comp_nm, comp_data in example_data.items() 
})

返回:

                       AMZN         GOOGL
improv_sharpe -4.971967e-05  2.611481e-05
stan_sharpe    5.995153e-01  1.632287e-01
prob_sharpe    1.015944e-19  6.376599e-72

您可以了解有关列表,字典和理解here的更多信息。