Создание таблицы в pandas из json - PullRequest
0 голосов
/ 06 мая 2020

Я действительно застрял в создании таблицы из вложенных json. Вывод json из запроса API Coinmarketcap:

data = {'status': {'timestamp': '2020-05-05T21:34:45.057Z', 'error_code': 0, 'error_message': None, 'elapsed': 8, 'credit_count': 1, 'notice': None}, 'data': {'1': {'urls': {'website': ['https://bitcoin.org/'], 'technical_doc': ['https://bitcoin.org/bitcoin.pdf'], 'twitter': [], 'reddit': ['https://reddit.com/r/bitcoin'], 'message_board': ['https://bitcointalk.org'], 'announcement': [], 'chat': [], 'explorer': ['https://blockchain.coinmarketcap.com/chain/bitcoin', 'https://blockchain.info/', 'https://live.blockcypher.com/btc/', 'https://blockchair.com/bitcoin', 'https://explorer.viabtc.com/btc'], 'source_code': ['https://github.com/bitcoin/']}, 'logo': 'https://s2.coinmarketcap.com/static/img/coins/64x64/1.png', 'id': 1, 'name': 'Bitcoin', 'symbol': 'BTC', 'slug': 'bitcoin', 'description': 'Bitcoin (BTC) is a consensus network that enables a new payment system and a completely digital currency. Powered by its users, it is a peer to peer payment network that requires no central authority to operate. On October 31st, 2008, an individual or group of individuals operating under the pseudonym "Satoshi Nakamoto" published the Bitcoin Whitepaper and described it as: "a purely peer-to-peer version of electronic cash, which would allow online payments to be sent directly from one party to another without going through a financial institution."', 'notice': None, 'date_added': '2013-04-28T00:00:00.000Z', 'tags': ['mineable'], 'tag-names': ['Mineable'], 'tag-groups': ['APPLICATION'], 'is_hidden': 0, 'platform': None, 'category': 'coin'}, '2': {'urls': {'website': ['https://litecoin.org/'], 'technical_doc': [], 'twitter': ['https://twitter.com/LitecoinProject'], 'reddit': ['https://reddit.com/r/litecoin'], 'message_board': ['https://litecointalk.io/', 'https://litecoin-foundation.org/'], 'announcement': ['https://bitcointalk.org/index.php?topic=47417.0'], 'chat': ['https://telegram.me/litecoin'], 'explorer': ['https://blockchair.com/litecoin', 'https://chainz.cryptoid.info/ltc/', 'http://explorer.litecoin.net/chain/Litecoin', 'https://ltc.tokenview.com/en', 'https://explorer.viabtc.com/ltc'], 'source_code': ['https://github.com/litecoin-project/litecoin']}, 'logo': 'https://s2.coinmarketcap.com/static/img/coins/64x64/2.png', 'id': 2, 'name': 'Litecoin', 'symbol': 'LTC', 'slug': 'litecoin', 'description': 'Litecoin is a peer-to-peer cryptocurrency created by Charlie Lee. It was created based on the Bitcoin protocol but differs in terms of the hashing algorithm used. Litecoin uses the memory intensive Scrypt proof of work mining algorithm. Scrypt allows consumer-grade hardware such as GPU to mine those coins.', 'notice': None, 'date_added': '2013-04-28T00:00:00.000Z', 'tags': ['mineable'], 'tag-names': ['Mineable'], 'tag-groups': ['APPLICATION'], 'is_hidden': 0, 'platform': None, 'category': 'coin'}}}

код:

result = pd.json_normalize(data,'data',['website'], errors='ignore')
print(result)

вывод:

0     website
0  1  NaN
1  2  NaN 

Я пытаюсь достичь чего-то вроде этого:

0     website
0  1  https://bitcoin.org/
1  2  https://litecoin.org/

Я перепробовал так много вещей и действительно разочаровался. Заранее спасибо!

1 Ответ

0 голосов
/ 06 мая 2020

Вам нужно немного обработать данные, чтобы получить то, что вы хотите.

(
    pd.DataFrame(data['data'])
    .loc['urls']
    .apply(lambda x: x['website'][0])
    .to_frame('website')
)

    website
1   https://bitcoin.org/
2   https://litecoin.org/

Чтобы получить Technical_do c, вы можете сделать нечто подобное. Причина, по которой вы получили ошибку, заключается в том, что для некоторых элементов нет технической документации c.

(
    pd.DataFrame(data['data'])
    .loc['urls']
    .apply(lambda x: (x['technical_doc']+[''])[0])
    .to_frame('website')
)

Чтобы получить lo go, вам нужно lo c 'lo go' вместо 'urls', поскольку он находится на том же уровне, что и 'urls'

(
    pd.DataFrame(data['data'])
    .loc['logo']
    .to_frame('logo')
)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...