簡體   English   中英

將 CoinMarketCap API json 數據轉換為扁平化 pandas Z6A8064B5DF4794555005Z53C4D 的問題

[英]Problems converting CoinMarketCap API json data into flattened pandas dataframe

目標我成功地將 CMC 數據從 APi 導入到嵌套的 JSON 中,每個示例: https://coinmarketcap/getVapi/CryptocurrencyQuotes1/getVapi/

工作 API 調用

url = 'https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest'

parameters = { 
              'id': '2280,4558,5221,1518,7653,5026,10688,5632,5567,5692,5566,8075,7083', 
              'convert': 'USD' 
              } 

headers = {
        'Accepts': 'application/json',
        'X-CMC_PRO_API_KEY': CMC_AUTH
        } 

session = Session()
session.headers.update(headers)
response = session.get(url, params=parameters)
data = json.loads(response.text)
pprint.pprint(data)

結果

這將返回嵌套的 JSON ,如上面的示例所示,其中的數據略有不同,文檔示例中尚未更新。

這是上面代碼返回的第一個id

{'data': {'10688': {'circulating_supply': 116331159.1611021,
                    'cmc_rank': 347,
                    'date_added': '2021-06-28T00:00:00.000Z',
                    'id': 10688,
                    'is_active': 1,
                    'is_fiat': 0,
                    'last_updated': '2022-09-05T19:20:00.000Z',
                    'max_supply': 1000000000,
                    'name': 'Yield Guild Games',
                    'num_market_pairs': 98,
                    'platform': {'id': 1027,
                                 'name': 'Ethereum',
                                 'slug': 'ethereum',
                                 'symbol': 'ETH',
                                 'token_address': '0x25f8087ead173b73d6e8b84329989a8eea16cf73'},
                    'quote': {'USD': {'fully_diluted_market_cap': 482405830.29,
                                      'last_updated': '2022-09-05T19:20:00.000Z',
                                      'market_cap': 56118829.42337308,
                                      'market_cap_dominance': 0.0057,
                                      'percent_change_1h': 0.43811262,
                                      'percent_change_24h': -3.21631892,
                                      'percent_change_30d': -40.69530618,
                                      'percent_change_60d': -32.68437619,
                                      'percent_change_7d': -8.8136777,
                                      'percent_change_90d': -16.90500242,
                                      'price': 0.48240583028710726,
                                      'tvl': None,
                                      'volume_24h': 11205334.0356885,
                                      'volume_change_24h': -48.174}},
                    'self_reported_circulating_supply': 87906250,
                    'self_reported_market_cap': 42406487.51867602,
                    'slug': 'yield-guild-games',
                    'symbol': 'YGG',
                    'tags': [{'category': 'CATEGORY',
                              'name': 'Collectibles & NFTs',
                              'slug': 'collectibles-nfts'},
                             {'category': 'INDUSTRY',
                              'name': 'Gaming',
                              'slug': 'gaming'},
                             {'category': 'INDUSTRY',
                              'name': 'Entertainment',
                              'slug': 'entertainment'},
                             {'category': 'CATEGORY',
                              'name': 'DAO',
                              'slug': 'dao'},
                             {'category': 'CATEGORY',
                              'name': 'Metaverse',
                              'slug': 'metaverse'},
                             {'category': 'CATEGORY',
                              'name': 'a16z Portfolio',
                              'slug': 'a16z-portfolio'},
                             {'category': 'CATEGORY',
                              'name': 'Play To Earn',
                              'slug': 'play-to-earn'},
                             {'category': 'CATEGORY',
                              'name': 'Animoca Brands Portfolio',
                              'slug': 'animoca-brands-portfolio'},
                             {'category': 'CATEGORY',
                              'name': 'Gaming Guild',
                              'slug': 'gaming-guild'},
                             {'category': 'CATEGORY',
                              'name': 'OKEx Blockdream Ventures Portfolio',
                              'slug': 'okex-blockdream-ventures-portfolio'}],
                    'total_supply': 1000000000,
                    'tvl_ratio': None},

我現在想要展平 json 數據並將其轉換為 pandas dataframe,所以我將上面的最后一行更改為:

新代碼

df = pd.json_normalize(data)
pprint.pprint(df)

結果

這會產生一個包含 454 列的單行,而 dataframe 則包含我指定的所有 13 個 id 的行和相應的列值。

從我讀過的內容來看, json_normalize 應該是展平數據的方法,我認為問題是指定我想要展平嵌套的quote數據,我可能應該刪除字典的嵌套tags列表,但我沒有找到有關如何執行此操作的任何信息。

我嘗試將record_path參數添加到json_normalizequote ,但這只會返回KeyError: 'quote'

我沒有找到任何其他信息來解決此問題,並且非常感謝有關如何解決此問題的任何指導。

您可以使用此示例展平從問題中的 Json 加載的 dataframe ( dct包含字典):

df = pd.DataFrame(dct["data"]).T.explode("tags")
df = pd.concat(
    [df, df.pop("platform").apply(pd.Series).add_prefix("platform_")], axis=1
)
df = pd.concat(
    [df, df.pop("quote").apply(pd.Series).add_prefix("quote_")], axis=1
)
df = pd.concat([df, df.pop("tags").apply(pd.Series).add_prefix("tag_")], axis=1)

print(df)

印刷:

      circulating_supply cmc_rank                date_added     id is_active is_fiat              last_updated  max_supply               name num_market_pairs self_reported_circulating_supply self_reported_market_cap               slug symbol total_supply tvl_ratio  platform_id platform_name platform_slug platform_symbol                      platform_token_address                                                                                                                                                                                                                                                                                                                                                                                                                                                                          quote_USD tags_category                           tags_name                           tags_slug
10688   116331159.161102      347  2021-06-28T00:00:00.000Z  10688         1       0  2022-09-05T19:20:00.000Z  1000000000  Yield Guild Games               98                         87906250          42406487.518676  yield-guild-games    YGG   1000000000      None         1027      Ethereum      ethereum             ETH  0x25f8087ead173b73d6e8b84329989a8eea16cf73  {'fully_diluted_market_cap': 482405830.29, 'last_updated': '2022-09-05T19:20:00.000Z', 'market_cap': 56118829.42337308, 'market_cap_dominance': 0.0057, 'percent_change_1h': 0.43811262, 'percent_change_24h': -3.21631892, 'percent_change_30d': -40.69530618, 'percent_change_60d': -32.68437619, 'percent_change_7d': -8.8136777, 'percent_change_90d': -16.90500242, 'price': 0.48240583028710726, 'tvl': None, 'volume_24h': 11205334.0356885, 'volume_change_24h': -48.174}      CATEGORY                 Collectibles & NFTs                   collectibles-nfts
10688   116331159.161102      347  2021-06-28T00:00:00.000Z  10688         1       0  2022-09-05T19:20:00.000Z  1000000000  Yield Guild Games               98                         87906250          42406487.518676  yield-guild-games    YGG   1000000000      None         1027      Ethereum      ethereum             ETH  0x25f8087ead173b73d6e8b84329989a8eea16cf73  {'fully_diluted_market_cap': 482405830.29, 'last_updated': '2022-09-05T19:20:00.000Z', 'market_cap': 56118829.42337308, 'market_cap_dominance': 0.0057, 'percent_change_1h': 0.43811262, 'percent_change_24h': -3.21631892, 'percent_change_30d': -40.69530618, 'percent_change_60d': -32.68437619, 'percent_change_7d': -8.8136777, 'percent_change_90d': -16.90500242, 'price': 0.48240583028710726, 'tvl': None, 'volume_24h': 11205334.0356885, 'volume_change_24h': -48.174}      INDUSTRY                              Gaming                              gaming
10688   116331159.161102      347  2021-06-28T00:00:00.000Z  10688         1       0  2022-09-05T19:20:00.000Z  1000000000  Yield Guild Games               98                         87906250          42406487.518676  yield-guild-games    YGG   1000000000      None         1027      Ethereum      ethereum             ETH  0x25f8087ead173b73d6e8b84329989a8eea16cf73  {'fully_diluted_market_cap': 482405830.29, 'last_updated': '2022-09-05T19:20:00.000Z', 'market_cap': 56118829.42337308, 'market_cap_dominance': 0.0057, 'percent_change_1h': 0.43811262, 'percent_change_24h': -3.21631892, 'percent_change_30d': -40.69530618, 'percent_change_60d': -32.68437619, 'percent_change_7d': -8.8136777, 'percent_change_90d': -16.90500242, 'price': 0.48240583028710726, 'tvl': None, 'volume_24h': 11205334.0356885, 'volume_change_24h': -48.174}      INDUSTRY                       Entertainment                       entertainment

...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM