I am trying to make a dataframe with Historical data of daily No. of stock Advancing and declining with their respective volumes of Nifty 50 index. Being new to python I am having trouble handling pandas dataframe and conditions.
Below is the code that I wrote, but it has a lot of issues:
df.index = data.index error:ValueError: Length mismatch: Expected axis has 0 elements, new values have 248 elements
if I comment out the above line where I set the index of the empty dataframe, the code runs and gives an empty Dataframe at the end.
#setting default dates end_date = date.today() start_date = end_date - timedelta(365) #Deriving the names of 50 stocks in Nifty 50 Index nifty_50 = pd.read_html('https://en.wikipedia.org/wiki/NIFTY_50') nifty50_symbols = nifty_50[1][1] df = pd.DataFrame(columns = {'Advances','Declines','Adv_Volume','Dec_Volume'}) for x in nifty50_symbols: data = nsepy.get_history(symbol = x, start=start_date, end=end_date) sclose = data['Close'] sopen = data['Open'] svol = data['Volume'] ## df.index = data.index ## for i in df.index: --- since df.index was commented out it's value was nill for i in data.index: if sclose > sopen: df['Advances'] = df['Advances'] + 1 df['Adv_Volume'] = df['Adv_Volume'] + svol elif sopen > sclose: df['Declines'] = df['Declines'] + 1 df['Dec_Volume'] = df['Dec_Volume'] + svol print(df.tail())
Output :
Empty DataFrame
Columns: [Dec_Volume, Declines, Advances, Adv_Volume]
Index: []
EDIT: Found the reason why the code was giving an empty dataframe, because df.index was nill, so the if statement was never triggered.When I changed that part into data.index if statement was triggered. But Now I do not know how do I use the IF statements, since it is giving the error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
EDIT2: Updated Code with the help of Akshay Nevrekar: Still getting an empty dataframe at the end. Also I have to set index of DF as the dates in data.index, so that I can later relate the Advances/declines to their respective dates.
#setting default dates
end_date = date.today()
start_date = end_date - timedelta(365)
#Deriving the names of 50 stocks in Nifty 50 Index
nifty_50 = pd.read_html('https://en.wikipedia.org/wiki/NIFTY_50')
nifty50_symbols = nifty_50[1][1]
df = pd.DataFrame(columns = {'Advances','Declines','Adv_Volume','Dec_Volume'})
for x in nifty50_symbols:
data = ns.get_history(symbol = x, start=start_date, end=end_date)
## sclose = data['Close']
## sopen = data['Open']
## svol = data['Volume']
## df.index = data.index
for i in data.index:
sclose=data.loc[i]['Close']
sopen=data.loc[i]['Open']
svol = data.loc[i]['Volume']
if sclose > sopen :
df['Advances'] = df['Advances'] + 1
df['Adv_Volume'] = df['Adv_Volume'] + svol
elif sopen > sclose :
df['Declines'] = df['Declines'] + 1
df['Dec_Volume'] = df['Dec_Volume'] + svol
print(df
)