1

I have following code

import requests
import json
import pandas as pd
from datetime import datetime
from datetime import timedelta
pd.options.display.float_format = '{:,.2f}'.format

url="https://nseindia.com/api/equity-stockIndices?index=SECURITIES%20IN%20F%26O"

headers = { "Accept-Encoding":"gzip, deflate","Accept-Language":"en-US,en;q=0.9",
            "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36 OPR/65.0.3467.78"}

r=requests.get(url, headers=headers).json()
df1=pd.DataFrame().from_records(r['data'])
df2=df1[['symbol','dayHigh','dayLow','lastPrice','yearHigh','yearLow','previousClose','change','pChange']]
df2=df2.sort_values(['symbol'], ascending=True).reset_index(drop=True)
nifty1=df2.round(2)
nifty1
dt1 = '01-01-2020'
dt2 = '11-01-2020'

Generating following output

    symbol  dayHigh dayLow  lastPrice   yearHigh    yearLow previousClose   change  pChange
0   ACC 1525    1487.25 1510.8  1769.05 1326    1486.15 24.65   1.66
1   ADANIENT    213 205.9   210.5   221.5   113 208 2.50    1.20
2   ADANIPORTS  394.3   390 391.3   430.6   292.1   391.8   -0.50   -0.13
...

I have following string for

https://nseindia.com/api/historical/cm/equity?symbol=XXXX&series=["EQ"]&from=fromdt&to=todt

I want to replace XXXX by nifty1['symbol'] column, preferably without space and fromdt and todt with dt1 and dt2

eg.

https://nseindia.com/api/historical/cm/equity?symbol=ACC&series=["EQ"]&from=01-01-2020&to=11-01-2020
https://nseindia.com/api/historical/cm/equity?symbol=ADANIENT&series=["EQ"]&from=01-01-2020&to=11-01-2020
https://nseindia.com/api/historical/cm/equity?symbol=ADANIPORTS&series=["EQ"]&from=01-01-2020&to=11-01-2020

and store into new dataframe

1 Answer 1

1

Here's a way to do using f-strings:

new_df = (df['symbol']
          .apply(lambda x: f'https://nseindia.com/api/historical/cm/equity?symbol={x}&series=["EQ"]&from=fromdt&to=todt')
          .to_frame())
Sign up to request clarification or add additional context in comments.

4 Comments

thanks a lot but it is having 2 issues 1) I have to also replace fromdt and todt with variable values e.g. from=01-01-2020&to=11-01-2020 and secondly it is not left justified
modification of code solved the first issue from={dt1}&to={dt2}
I just used the string as is given in the question, hope it helped.
surely it help Thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.