I'm trying to take the symbol of a stock and return it's open value but it doesn't ever recognize the stock. I am very new to pandas so I have no idea how to handle this.
def getOpen(symbol):
try:
return stock['Open', symbol]
except:
pass
finally:
return "I couldn't find that stock"
data set: Symbol,Open,High,Low,LTP,Chng,% Chng,Volume (lacs),Turnover (crs.),52w H,52w L,365 d % chng,30 d % chng ADANIPORTS,750,766,713.25,715,-47.45,-6.22,72.2,532.63,901,384.4,79.22,-4.65 ASIANPAINT,"3,101.00","3,167.35","3,091.00","3,138.00",-6.25,-0.2,10.29,322.53,"3,505.00","2,117.15",45.66,5.66
CodePudding user response:
Please share a small example of the dataset you are working on to help understand the question better. I'm not sure what are the rows and columns in your data set.
Basics to get around in pandas:
df['column_name'] - returns a subset of your data set just with column
df.loc['row_index','column_name'] - returns a value from a column and row using column name and row_index
in your case I think it will be:
stock.loc[symbol, 'Open'] - if 'Open' is a column and symbols are in rows or stock.loc['Open', symbol]- if symbols are in columns and 'Open' is a row
Based on the comments I can see that both 'Symbol' and 'Open' are columns,try this:
def getOpen(symbol):
try:
return stock.loc[stock['Symbol'] == symbol,'Open']
except:
pass
finally:
return "I couldn't find that stock"
