简体   繁体   English

将数据从 JSON 转换为 pandas dataframe

[英]convert data from JSON to pandas dataframe

I would like to extract some data from the following line of text.我想从以下文本行中提取一些数据。 The data of interest are: 'exchange_symbol under both market_pair_base and 'market_pair_quote , all the data under exchange_reported .感兴趣的数据是: market_pair_base'market_pair_quote下的' exchange_reported'exchange_symbol下的所有数据。 Any help would be great.任何帮助都会很棒。

{'status': {'timestamp': '2019-10-04T02:57:39.238Z', 'error_code': 0, 'error_message': None, 'elapsed': 14, 'credit_count': 1, 'notice': None}, 'data': {'id': 112, 'name': 'Liquid', 'slug': 'liquid', 'num_market_pairs': 190, 'market_pairs': [{'market_pair_base': {'exchange_symbol': 'ETH', 'currency_id': 1027, 'currency_symbol': 'ETH', 'currency_type': 'cryptocurrency'}, 'market_pair_quote': {'exchange_symbol': 'JPY', 'currency_id': 2797, 'currency_symbol': 'JPY', 'currency_type': 'fiat'}, 'quote': {'exchange_reported': {'price': 18522.8757, 'volume_24h_base': 12901.4143123, 'volume_24h_quote': 238971293.660934, 'last_updated': '2019-10-04T02:57:04.000Z'}, 'USD': {'price': 173.503768779353, 'volume_24h': 2238444.00576794, 'last_updated': '2019-10-04T02:57:04.000Z'}}, 'market_id': 4431, 'market_pair': 'ETH/JPY', 'category': 'spot', 'fee_type': 'percentage'}, {'market_pair_base': {'exchange_symbol': 'XRP', 'currency_id': 52, 'currency_symbol': 'XRP', 'currency_type': 'cryptocurrency'}, 'market_pair_quote': {'exchange_symbol': 'JPY', 'currency_id': 2797, 'currency_symbol': 'JPY', 'currency_type': 'fiat'}, 'quote': {'exchange_reported': {'price': 26.55199, 'volume_24h_base': 8223150.63965144, 'volume_24h_quote': 218341013.552519, 'last_updated': '2019-10-04T02:56:04.000Z'}, 'USD': {'price': 0.248712479001935, 'volume_24h': 2045200.18079406, 'last_updated': '2019-10-04T02:56:04.000Z'}}, 'market_id': 16254, 'market_pair': 'XRP/JPY', 'category': 'spot', 'fee_type': 'percentage'}]}}

I tried the following code but this send an error:我尝试了以下代码,但这会发送错误:

    File "proc_manual_comp.py", line 112, in <module>
     for item in data['data'][currency]['market_pairs']
  TypeError: 'int' object is not subscriptable

the code:编码:

c_price = []
url = 'https://pro-api.coinmarketcap.com/v1/exchange/market-pairs/latest'
parameters = {
   'id':'112',
   'start':'1',
   'limit':'91'
 }
 headers = {
   'Accepts': 'application/json',
   'X-CMC_PRO_API_KEY': 'XXXXXXXXXXXXXXXXXXXXXXX',
 }
 session = Session()
 session.headers.update(headers)
 response = session.get(url, params=parameters)
 data = json.loads(response.text)
 for currency in  data['data']:
   used_list = [ 
        item['market_pairs']['market_pair_base'] 
        for item in data['data'][currency]['market_pairs']
   ]
   price = pd.DataFrame.from_records(used_list)
   print(price)
   price['timestamp'] = pd.to_datetime(price['timestamp'])
   price['timestamp'] = price['timestamp'].astype(str).str[:-6]
   price_c = price.set_index('timestamp').close
   c_price.append(price_c.rename(currency))
   print(c_price)
  c_price = pd.concat(c_price, axis=1)

Expected output:预期 output:

market_pair_base  market_pair_quoted   last_updated             price
ETH               JPY                  2019-10-04T02:57:04.000Z 18522.8757

it seems like a problem with your for loop.您的 for 循环似乎有问题。

for currency in  data['data']:

here since data['data'] is a dict hence values of currency is going to be keys of dict, an alternate way can be something like this在这里,因为 data['data'] 是一个字典,因此货币的值将是字典的键,另一种方式可以是这样的

for market_pairs in data['data']['market_pairs']:
    used_list = [market_pair['market_pair_base'] for market_pair in market_pairs]
    price = pd.DataFrame.from_records(used_list)

this is based on the fact that you wanted to put all market_pair_base in a Dataframe.这是基于您想将所有market_pair_base放入 Dataframe 的事实。

Assumed JSON as given in d variable假设 JSON 如在d变量中给出

Try below snippet:试试下面的片段:

target_df=pd.DataFrame(columns=['market_pair_base','market_pair_quote','price','last_updated'])
target=dict()

usedlist=d['data']['market_pairs']

for i in range(len(usedlist)):
    target['market_pair_base']=[usedlist[i]['market_pair_base']['exchange_symbol']]
    target['market_pair_quote'] = [usedlist[i]['market_pair_quote']['exchange_symbol']]
    target['price'] = [usedlist[i]['quote']['exchange_reported']['price']]
    target['last_updated'] = [usedlist[i]['quote']['exchange_reported']['last_updated']]
    target_df=pd.concat([target_df, pd.DataFrame(target)], ignore_index=True)

This print(target_df) will give desired output as below:print(target_df)将给出所需的 output 如下:

market_pair_base market_pair_quote        price              last_updated
0              ETH               JPY  18522.87570  2019-10-04T02:57:04.000Z
1              XRP               JPY     26.55199  2019-10-04T02:56:04.000Z

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM