How to change Json data output in table format

Question:

import requests
from pprint import  pprint
import pandas as pd

baseurl = "https://www.nseindia.com/"
url = f'https://www.nseindia.com/api/live-analysis-oi-spurts-underlyings'
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, '
                         'like Gecko) '
                         'Chrome/80.0.3987.149 Safari/537.36',
           'accept-language': 'en,gu;q=0.9,hi;q=0.8', 'accept-encoding': 'gzip, deflate, br'}
session = requests.Session()
request = session.get(baseurl, headers=headers, timeout=30)
cookies = dict(request.cookies)
res = session.get(url, headers=headers, timeout=30, cookies=cookies)

print(res.json())

I tried df = pd.DataFrame(res.json()) but couldn’t get data in table format. How to do that Plz. Also how to select few particular columns only in data output instead of all columns.

Asked By: Brijesh Chaurasia

||

Answers:

Try this :

import json
import codecs

df = pd.DataFrame(json.loads(codecs.decode(bytes(res.text, 'utf-8'), 'utf-8-sig'))['data'])

>>> print(df)

enter image description here

Answered By: L'Artiste
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.