Home > front end >  Python pandas - too many info in 1 part of this Json
Python pandas - too many info in 1 part of this Json

Time:09-26

I managed to get info from this json using request, but i only want the Max Leverage part. there is too many info in the leverage_filter part.

bybit = requests.get('https://api.bybit.com/v2/public/symbols')
e = bybit.json()
#print(e)
df = pd.DataFrame(e['result'], columns=['name','leverage_filter'])
df = df[df['name'].str.endswith('USDT')]
print(df)

the result i get is this:

             name                                    leverage_filter
10        BTCUSDT  {'min_leverage': 1, 'max_leverage': 100, 'leve...
11        ETHUSDT  {'min_leverage': 1, 'max_leverage': 100, 'leve...
12        EOSUSDT  {'min_leverage': 1, 'max_leverage': 50, 'lever...

What my expected result is:

             name  leverage_filter
10        BTCUSDT              100
11        ETHUSDT              100
12        EOSUSDT              50

CodePudding user response:

You can use .str accessor to get value of max_leverage

df['leverage_filter'] = df['leverage_filter'].str['max_leverage'] 

You can also use jmespath to query the json first

import jmespath
expression = jmespath.compile('[].{name:name, max_leverage:leverage_filter.max_leverage}')

out = pd.DataFrame(expression.search(e['result']))
print(out)

          name  max_leverage
0       BTCUSD           100
1       ETHUSD           100
2       EOSUSD            50
3       XRPUSD            50
4       DOTUSD            50
  • Related