为了账号安全,请及时绑定邮箱和手机立即绑定

如何将多个 json/ python 字典合并到 1 个数据帧中

如何将多个 json/ python 字典合并到 1 个数据帧中

当年话下 2023-07-18 15:10:16
我有以下从 API 调用中获取的 json 文件,我希望能够将数据合并到 1 个数据帧中,以便我可以使用 pandas 将其写入 csv 文件。原始 JSON{'country': 'US', 'currency': 'USD', 'exchange': 'NEW YORK STOCK EXCHANGE, INC.', 'finnhubIndustry': 'Life Sciences Tools & Services', 'ipo': '1999-11-18', 'logo': 'https://static.finnhub.io/logo/5f1f8412-80eb-11ea-bd05-00000000092a.png', 'marketCapitalization': 30719.97, 'name': 'Agilent Technologies Inc', 'phone': '14083458886', 'shareOutstanding': 308.309635, 'ticker': 'A', 'weburl': 'https://www.agilent.com/'}{'country': 'US', 'currency': 'USD', 'exchange': 'NEW YORK STOCK EXCHANGE, INC.', 'finnhubIndustry': 'Metals & Mining', 'ipo': '2016-10-18', 'logo': '', 'marketCapitalization': 2727.509, 'name': 'Alcoa Corp', 'phone': '14123152900', 'shareOutstanding': 185.924291, 'ticker': 'AA', 'weburl': 'https://www.alcoa.com/global/en/home.asp'}{'country': 'CN', 'currency': 'CNY', 'exchange': 'NASDAQ NMS - GLOBAL MARKET', 'finnhubIndustry': 'Diversified Consumer Services', 'ipo': '2008-01-29', 'logo': '', 'marketCapitalization': 35.81037, 'name': 'ATA Creativity Global', 'phone': '861065181133', 'shareOutstanding': 47.592384, 'ticker': 'AACG', 'weburl': 'http://www.ata.net.cn'}这就是我试图让数据做的事情ticker(as index)   country   currency   exchange   finnhubIndustry    ipo      logo   ...    A                 'US'      'USD'      'NEW YORK.. 'Life Science..'   1999-11  'http://..AA                'US'      'USD'      'NYSE'      'Metals & Mi...'   2016-10  'http://..AACG              'CN'      'CNY'      'NASDAQ'    'Diversified...'   2008-01  'http://..AADR              'US'      'USD'      'NASDAQ'    'N/A'              ''       'http://..cols = ['country', 'currency', 'exchange', 'finnhubIndustry', 'ipo', 'logo', 'marketCapitalization', 'name', 'phone', 'shareOutstanding', 'ticker', 'weburl']我不确定我的最佳路线是否是尝试将 json 数据组合成这样,然后进行转换,或者是否有更简单的方法。
查看完整描述

1 回答

?
偶然的你

TA贡献1841条经验 获得超3个赞

我想知道你的“原始 json”是否真的是你的意思。通常,一个 json 文件包含一个对象,在您的示例中为 4。我宁愿期望您的原始 json 文件类似于


[

{'country': 'US', 'currency': 'USD', 'exchange': 'NEW YORK STOCK EXCHANGE, INC.', 'finnhubIndustry': 'Life Sciences Tools & Services', 'ipo': '1999-11-18', 'logo': 'https://static.finnhub.io/logo/5f1f8412-80eb-11ea-bd05-00000000092a.png', 'marketCapitalization': 30719.97, 'name': 'Agilent Technologies Inc', 'phone': '14083458886', 'shareOutstanding': 308.309635, 'ticker': 'A', 'weburl': 'https://www.agilent.com/'},

{'country': 'US', 'currency': 'USD', 'exchange': 'NEW YORK STOCK EXCHANGE, INC.', 'finnhubIndustry': 'Metals & Mining', 'ipo': '2016-10-18', 'logo': '', 'marketCapitalization': 2727.509, 'name': 'Alcoa Corp', 'phone': '14123152900', 'shareOutstanding': 185.924291, 'ticker': 'AA', 'weburl': 'https://www.alcoa.com/global/en/home.asp'},

{'country': 'CN', 'currency': 'CNY', 'exchange': 'NASDAQ NMS - GLOBAL MARKET', 'finnhubIndustry': 'Diversified Consumer Services', 'ipo': '2008-01-29', 'logo': '', 'marketCapitalization': 35.81037, 'name': 'ATA Creativity Global', 'phone': '861065181133', 'shareOutstanding': 47.592384, 'ticker': 'AACG', 'weburl': 'http://www.ata.net.cn'},

{'country': 'US', 'currency': 'USD', 'exchange': 'NASDAQ NMS - GLOBAL MARKET', 'finnhubIndustry': 'N/A', 'ipo': '', 'logo': '', 'marketCapitalization': 738.99, 'name': 'Artius Acquisition Inc', 'phone': '12123097668', 'shareOutstanding': 87.54375, 'ticker': 'AACQU', 'weburl': ''}

]

这不是一个对象数组。或者您可能想要拥有多个 json 文件,每个文件都有一个对象。根据您的文件格式,您可以使用pandas.read_json


但是,如果您了解如何将对象整理为 Python 字典列表,则可以直接使用pandas.DataFrame它来创建它。它将完全像你想要的那样:


>>> x = [

... {'country': 'US', 'currency': 'USD', 'exchange': 'NEW YORK STOCK EXCHANGE, INC.', 'finnhubIndustry': 'Life Sciences Tools & Services', 'ipo': '1999-11-18', 'logo': 'https://static.finnhub.io/logo/5f1f8412-80eb-11ea-bd05-00000000092a.png', 'marketCapitalization': 30719.97, 'name': 'Agilent Technologies Inc', 'phone': '14083458886', 'shareOutstanding': 308.309635, 'ticker': 'A', 'weburl': 'https://www.agilent.com/'},

... {'country': 'US', 'currency': 'USD', 'exchange': 'NEW YORK STOCK EXCHANGE, INC.', 'finnhubIndustry': 'Metals & Mining', 'ipo': '2016-10-18', 'logo': '', 'marketCapitalization': 2727.509, 'name': 'Alcoa Corp', 'phone': '14123152900', 'shareOutstanding': 185.924291, 'ticker': 'AA', 'weburl': 'https://www.alcoa.com/global/en/home.asp'},

... {'country': 'CN', 'currency': 'CNY', 'exchange': 'NASDAQ NMS - GLOBAL MARKET', 'finnhubIndustry': 'Diversified Consumer Services', 'ipo': '2008-01-29', 'logo': '', 'marketCapitalization': 35.81037, 'name': 'ATA Creativity Global', 'phone': '861065181133', 'shareOutstanding': 47.592384, 'ticker': 'AACG', 'weburl': 'http://www.ata.net.cn'},

... {'country': 'US', 'currency': 'USD', 'exchange': 'NASDAQ NMS - GLOBAL MARKET', 'finnhubIndustry': 'N/A', 'ipo': '', 'logo': '', 'marketCapitalization': 738.99, 'name': 'Artius Acquisition Inc', 'phone': '12123097668', 'shareOutstanding': 87.54375, 'ticker': 'AACQU', 'weburl': ''}

... ]

>>> pandas.DataFrame(x)

  country currency  ... ticker                                    weburl

0      US      USD  ...      A                  https://www.agilent.com/

1      US      USD  ...     AA  https://www.alcoa.com/global/en/home.asp

2      CN      CNY  ...   AACG                     http://www.ata.net.cn

3      US      USD  ...  AACQU                                          


[4 rows x 12 columns]


查看完整回答
反对 回复 2023-07-18
  • 1 回答
  • 0 关注
  • 84 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信