Home > Software engineering >  Data ino pd.DataFrame
Data ino pd.DataFrame

Time:09-09

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".

CodePudding user response:

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)
df1 = pd.DataFrame.from_dict(json.loads(codecs.decode(bytes(res.text, 'utf-8'), 'utf-8-sig'))['data'])

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"]]
        
df_list = []
for i in a:
    temp = importdata(i)
    temp_df = pd.DataFrame(temp)
    df_list.append(temp_df)
    
df = pd.concat(df_list)
print (df)

CodePudding user response:

Change the line to:

df = pd.DataFrame([a[i]]) 
  • Related