gpt4 book ai didi

python - 从包含键和字典项嵌套列表的字典创建 Pandas DataFrame

转载 作者:行者123 更新时间:2023-12-04 08:04:07 26 4
gpt4 key购买 nike

我希望找到我面临的问题的答案。我目前正在尝试为一个项目抓取财务数据,并且正在创建 DataFrame 来存储我的数据以供下游操作的最后一步。
我的问题是我创建了一个键值对字典,其中键是公司符号,值对是一个列表,其中包含与该公司的市场数据相对应的键值对嵌套字典。以下是原始数据的示例。

{'MMM': [{'Market Cap': '102.234B'},
{'Beta (5Y Monthly)': '0.95'},
{'PE Ratio (TTM)': '19.09'},
{'EPS (TTM)': '9.25'},
{'Earnings Date': 'Apr 26, 2021 - Apr 30, 2021'},
{'Forward Dividend & Yield': '5.92 (3.35%)'},
{'Ex-Dividend Date': 'Feb 11, 2021'},
{'1y Target Est': '183.43'}],
'AXP': [{'Market Cap': '106.104B'},
{'Beta (5Y Monthly)': '1.28'},
{'PE Ratio (TTM)': '34.94'},
{'EPS (TTM)': '3.77'},
{'Earnings Date': 'Apr 22, 2021 - Apr 26, 2021'},
{'Forward Dividend & Yield': '1.72 (1.31%)'},
{'Ex-Dividend Date': 'Jan 07, 2021'},
{'1y Target Est': '130.32'}],
'AMGN': [{'Market Cap': '133.816B'},
{'Beta (5Y Monthly)': '0.73'},
{'PE Ratio (TTM)': '18.82'},
{'EPS (TTM)': '12.31'},

我试图使用 .from_dict 和索引方向创建一个 DataFrame ,但这是我得到的结果..
    0   1   2   3   4   5   6   7
MMM {'Market Cap': '102.234B'} {'Beta (5Y Monthly)': '0.95'} {'PE Ratio (TTM)': '19.09'} {'EPS (TTM)': '9.25'} {'Earnings Date': 'Apr 26, 2021 - Apr 30, 2021'} {'Forward Dividend & Yield': '5.92 (3.35%)'} {'Ex-Dividend Date': 'Feb 11, 2021'} {'1y Target Est': '183.43'}
AXP {'Market Cap': '106.104B'} {'Beta (5Y Monthly)': '1.28'} {'PE Ratio (TTM)': '34.94'} {'EPS (TTM)': '3.77'} {'Earnings Date': 'Apr 22, 2021 - Apr 26, 2021'} {'Forward Dividend & Yield': '1.72 (1.31%)'} {'Ex-Dividend Date': 'Jan 07, 2021'} {'1y Target Est': '130.32'}
AMGN {'Market Cap': '133.816B'} {'Beta (5Y Monthly)': '0.73'} {'PE Ratio (TTM)': '18.82'} {'EPS (TTM)': '12.31'} {'Earnings Date': 'Apr 28, 2021 - May 03, 2021'} {'Forward Dividend & Yield': '7.04 (3.04%)'} {'Ex-Dividend Date': 'Feb 11, 2021'} {'1y Target Est': '254.20'}
我将如何提取键/值对,以便键是列名,值是数据点?
任何帮助将不胜感激...谢谢

最佳答案

您可以先展平嵌套字典并传递给 DataFrame.from_dict :

d = {k: {k1: v1 for y in v for k1, v1 in y.items()} for k, v in d.items()}

df = pd.DataFrame.from_dict(d, orient='index')
将嵌套值转换为 Series 的解决方案然后使用 concat 转置:
df = pd.concat({k: pd.concat([pd.Series(y) for y in v]) for k, v in d.items()}, axis=1).T
print (df)
Market Cap Beta (5Y Monthly) PE Ratio (TTM) EPS (TTM) \
MMM 102.234B 0.95 19.09 9.25
AXP 106.104B 1.28 34.94 3.77

Earnings Date Forward Dividend & Yield Ex-Dividend Date \
MMM Apr 26, 2021 - Apr 30, 2021 5.92 (3.35%) Feb 11, 2021
AXP Apr 22, 2021 - Apr 26, 2021 1.72 (1.31%) Jan 07, 2021

1y Target Est
MMM 183.43
AXP 130.32

关于python - 从包含键和字典项嵌套列表的字典创建 Pandas DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66306846/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com