Data ino pd.DataFrame

Question:

from datetime import timedelta, date
from nsepy import get_history
import pandas as pd

def importdata(stock):
    stock_fut = get_history(symbol=stock,
           start=date.today() - timedelta(days = 3),           end=date.today(),
           futures=True,
           expiry_date=date(2022,9,29))    
   
    #print(stock_fut.columns)
    print(stock_fut[["Symbol","Number of Contracts","Change in OI","Open Interest"]])
    
a = ["ULTRACEMCO"]
    
#a = ["CONCOR", "JKCEMENT","SHREECEM","RAMCOCEM","INDIGO","ACC","BAJAJ-AUTO","ULTRACEMCO","PERSISTENT","MARUTI"]

for i in range(0,len(a)):
   #print(a[i])
   #importdata(a[i])
   df = pd.DataFrame(a[i]) 
   print(df)

Unable to do same with error as DataFrame is not called properly.
Also I want to join all data for all symbols in single table.

import requests
import json
import codecs
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)
df = pd.DataFrame(json.loads(codecs.decode(bytes(res.text, 'utf-8'), 'utf-8-sig'))['data'])
mini_df = df[['symbol']]
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
#print(df)
#print(mini_df)
print(mini_df.to_string(index=False))

Sir what if I want this symbol output to give input in below code for value of "a".

Asked By: Brijesh Chaurasia

||

Answers:

Change the line to:

df = pd.DataFrame([a[i]]) 
Answered By: the_ordinary_guy

I tried to fix your code with minimal modification. Hope it helps;

from datetime import timedelta, date
from nsepy import get_history
import pandas as pd
import requests
import json
import codecs

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)

# hereby we use from_dict module of pandas.DataFrame in order to make a dataframe from a collection of dictionaries inside a dictionary.
df1 = pd.DataFrame.from_dict(json.loads(codecs.decode(bytes(res.text, 'utf-8'), 'utf-8-sig'))['data'])

# and here we get a list unique of symbols
a = df1.symbol.unique().tolist()


def importdata(stock):
    stock_fut = get_history(symbol=stock,
           start=date.today() - timedelta(days = 3),           end=date.today(),
           futures=True,
           expiry_date=date(2022,9,29))    
   
    return stock_fut[["Symbol","Number of Contracts","Change in OI","Open Interest"]]
        
# here we add all the dataframes to a list
df_list = []
for i in a:
    temp = importdata(i)
    temp_df = pd.DataFrame(temp)
    df_list.append(temp_df)
# and here we concatenate all of them together in a row-wise manner.    
df = pd.concat(df_list)
print (df)
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.