Home > Mobile >  Get a data frame from a part of a string
Get a data frame from a part of a string

Time:12-15

I have the market information as a string like this:

{"POLISUSDT": {"name": "POLISUSDT", "min_amount": "0.5", "maker_fee_rate": "0.003", "taker_fee_rate": "0.003", "pricing_name": "USDT", "pricing_decimal": 4, "trading_name": "POLIS", "trading_decimal": 8}, "XPRBTC": {"name": "XPRBTC", "min_amount": "100", "maker_fee_rate": "0.003", "taker_fee_rate": "0.003", "pricing_name": "BTC", "pricing_decimal": 10, "trading_name": "XPR", "trading_decimal": 8}, "PSPUSDT": {"name": "PSPUSDT", "min_amount": "5", "maker_fee_rate": "0.003", "taker_fee_rate": "0.003", "pricing_name": "USDT", "pricing_decimal": 4, "trading_name": "PSP", "trading_decimal": 8}, "KAVAUSDT": {"name": "KAVAUSDT", "min_amount": "0.5", "maker_fee_rate": "0.003", "taker_fee_rate": "0.003", "pricing_name": "USDT", "pricing_decimal": 4, "trading_name": "KAVA", "trading_decimal": 8}, "CFXUSDT": {"name": "CFXUSDT", "min_amount": "10", "maker_fee_rate": "0.003", "taker_fee_rate": "0.003", "pricing_name": "USDT", "pricing_decimal": 6, "trading_name": "CFX", "trading_decimal": 8}}, "message": "OK"}

I want the information of each market, separately. For example, if I call "POLISUSDT", I want to get this data frame :

min_amount maker_fee_rate taker_fee_rate pricing_name pricing_decimal trading_name trading_decimal
0.5 0.003 0.003 USDT 4 POLIS 8

CodePudding user response:

After remove error data from sample is possible use dictioanry comprehension with json_normalize:

d ={"POLISUSDT": {"name": "POLISUSDT", "min_amount": "0.5", "maker_fee_rate": "0.003", "taker_fee_rate": "0.003", "pricing_name": "USDT", "pricing_decimal": 4, "trading_name": "POLIS", "trading_decimal": 8}, "XPRBTC": {"name": "XPRBTC", "min_amount": "100", "maker_fee_rate": "0.003", "taker_fee_rate": "0.003", "pricing_name": "BTC", "pricing_decimal": 10, "trading_name": "XPR", "trading_decimal": 8}, "PSPUSDT": {"name": "PSPUSDT", "min_amount": "5", "maker_fee_rate": "0.003", "taker_fee_rate": "0.003", "pricing_name": "USDT", "pricing_decimal": 4, "trading_name": "PSP", "trading_decimal": 8}, "KAVAUSDT": {"name": "KAVAUSDT", "min_amount": "0.5", "maker_fee_rate": "0.003", "taker_fee_rate": "0.003", "pricing_name": "USDT", "pricing_decimal": 4, "trading_name": "KAVA", "trading_decimal": 8}, "CFXUSDT": {"name": "CFXUSDT", "min_amount": "10", "maker_fee_rate": "0.003", "taker_fee_rate": "0.003", "pricing_name": "USDT", "pricing_decimal": 6, "trading_name": "CFX", "trading_decimal": 8}}

df = {k: pd.json_normalize(v) for k, v in d.items()}
print (df['POLISUSDT'])
        name min_amount maker_fee_rate taker_fee_rate pricing_name  \
0  POLISUSDT        0.5          0.003          0.003         USDT   

   pricing_decimal trading_name  trading_decimal  
0                4        POLIS                8  

CodePudding user response:

Please note I found some issues in parsing your json - there is an extra } just before the message field.

Leaving that aside, if you have the str representation of your json (let's call it json_str), you can load it in a big dataframe using:

import pandas as pd

dt = pd.read_json(json_str).transpose()

In this part of the code, you should drop the columns you are not interested in.

You can access any element using:

dt.loc[["POLISUSDT"]]
  • Related