KeyError for column that exits in dataframe - python

This works if I remove the schedule but if i leave it in i receive a key error for 'Symbol'
def tweet_and_archive(sl):
ticker_l = []
name_l = []
price_l = []
price_out_l = []
date_time = []
for index, row in sl.iterrows():
Stock = row['Symbol']
Price = row['Price']
Price_out = row['Price Out']
name_ = row['Name']
Date_ = row['DateTime']
if ...
schedule.every().monday.at('12:31').do(lambda: tweet_and_archive(short_list))
while True:
schedule.run_pending()
time.sleep(1)
This is the short_list dataframe:
Symbol Name Price % Change Price Out DateTime
0 ANGPY Anglo American Platinum Limited 25.82 7.14 NaN 28/02/2022

Related

streamlit st.columns to show live stock data

Problem statement:
I need multiple columns in streamlit to categorize different stocks and show their current live value.
I have 2 dummy columns as shown below (in the code) to track live values but the problem is instead of overwriting the current ticker value, it starts to append the column and write new values below the existing one.
import streamlit as st
import yfinance as yf
st.set_page_config(page_title="Test", layout='wide')
tech_list = ['btc-usd', 'eth-usd']
etf_list = ['etc-usd']
(tech_ticker, tech_price, etf_ticker, etf_price, crypto_ticker, crypto_price,
finance_ticker, finance_price, transport_ticker, transport_price) = st.columns(10, gap='small')
with tech_ticker:
for index, val in enumerate(tech_list):
st.write(val)
with etf_ticker:
for index, val in enumerate(etf_list):
st.write(val)
while True:
with tech_price:
number1 = st.empty()
with number1.container():
for index, val in enumerate(tech_list):
stock = yf.Ticker(val)
price = stock.info['regularMarketPrice']
st.write(": ", round(price, 1))
with etf_price:
number2 = st.empty()
with number2.container():
for index, val in enumerate(etf_list):
stock = yf.Ticker(val)
price = stock.info['regularMarketPrice']
st.write(": ", round(price, 1))
Put all the columns to an empty container:
import streamlit as st
import yfinance as yf
st.set_page_config(page_title="Test", layout='wide')
tech_list = ['btc-usd', 'eth-usd']
etf_list = ['etc-usd']
with st.empty():
while True:
(tech_ticker, tech_price, etf_ticker, etf_price, crypto_ticker,
crypto_price, finance_ticker, finance_price, transport_ticker,
transport_price) = st.columns(10, gap='small')
with tech_ticker:
for index, val in enumerate(tech_list):
st.write(val)
with etf_ticker:
for index, val in enumerate(etf_list):
st.write(val)
with tech_price:
for index, val in enumerate(tech_list):
stock = yf.Ticker(val)
price = stock.info['regularMarketPrice']
st.write(": ", round(price, 1))
with etf_price:
for index, val in enumerate(etf_list):
stock = yf.Ticker(val)
price = stock.info['regularMarketPrice']
st.write(": ", round(price, 1))
Define the holder outside the infinite loop.
# with etf_ticker:
# for index, val in enumerate(etf_list):
# st.write(val)
# Define holders.
with tech_price:
number1 = st.empty()
with etf_price:
number2 = st.empty()
# Update data - infinite loop
while True:
with number1.container():
for index, val in enumerate(tech_list):
stock = yf.Ticker(val)
price = stock.info['regularMarketPrice']
st.write(": ", round(price, 1))
with number2.container():
for index, val in enumerate(etf_list):
stock = yf.Ticker(val)
price = stock.info['regularMarketPrice']
st.write(": ", round(price, 1))
Output

Pandas how to search one df for a certain date and return that data

I have two data frames and I am trying to search each row by date in the user.csv file and find the corresponding date in the Raven.csv file and then return the Price from the df1 and the date and amount from df2.
This is working but my Price is returning a value like this [[0.11465]], is there a way to remove these brackets or a better way to do this?
import pandas as pd
df1 = pd.read_csv('Raven.csv',)
df2 = pd.read_csv('User.csv')
df1 = df1.reset_index(drop=False)
df1.columns = ['index', 'Date', 'Price']
df2['Timestamp'] = pd.to_datetime(df2['Timestamp'], format="%Y-%m-%d %H:%M:%S").dt.date
df1['Date'] = pd.to_datetime(df1['Date'], format="%Y-%m-%d").dt.date
Looper = 0
Date = []
Price = []
amount = []
total_value = []
for x in df2['Timestamp']:
search = df2['Timestamp'].values[Looper]
Date.append(search)
price =(df1.loc[df1['Date'] == search,['index']] )
value = df1['Price'].values[price]
Price.append(value)
payout = df2['Amount'].values[Looper]
amount.append(payout)
payout_value = value * payout
total_value.append(payout_value)
Looper = Looper + 1
dict = {'Date': Date, 'Price': Price, 'Payout': amount, "Total Value": total_value}
df = pd.DataFrame(dict)
df.to_csv('out.csv')
You can do indexing to get the value:
value = [[0.11465]][0][0]
print(value)
You get:
0.11465
I hope this is what you need.

Pandas dataframe check if string is not in column to append

I have a pandas dataframe with one of the columns called artist. I would like to append a new row only if the new artist name is not in this column.
I tried but with no success:
if (all_data != name.all(axis = 0)):
all_data = all_data.append({'artist':str(name), 'netWorth':str(worth.strip())}, ignore_index = True)
This is all the code I have:
def get_webpage(i, url):
URL = url+str(i)
response = requests.get(URL)
return bs4.BeautifulSoup(response.text, 'html.parser')
COLUMNS = ['artist', 'netWorth']
all_data = pd.DataFrame(columns = COLUMNS)
def scrape(soup):
artists = soup.find_all('article', class_ = 'thumb-wrap')
for ar in artists:
name = ar.h3.a.text
worth = ar.div.find('div', class_='bc-networth').text
global all_data
if (all_data['artist'] != name).any():
all_data = all_data.append({'artist':str(name), 'netWorth':str(worth.strip())}, ignore_index = True)
i = 1
url = 'http://www.therichest.com/celebnetworth-category/celeb/singer/page/'
while (i<=14):
soup = get_webpage(i, url)
i = i+1
data = scrape(soup)
i = 1
url = 'http://www.therichest.com/celebnetworth-category/celeb/musician/page/'
while (i<=7):
soup = get_webpage(i, url)
i = i+1
data = scrape(soup)
I believe need check one column artist only:
if (all_data['artist'] != str(name)).all():
Sample:
all_data = pd.DataFrame({'netWorth':[5,3],
'artist':list('ab')})
print (all_data)
netWorth artist
0 5 a
1 3 b
name = 'a'
b = 10
if (all_data['artist'] != str(name)).all():
all_data = all_data.append({'artist':str(name), 'netWorth':b }, ignore_index = True)
print (all_data)
netWorth artist
0 5 a
1 3 b
name = 'd'
b = 10
if (all_data['artist'] != name).all():
all_data = all_data.append({'artist':str(name), 'netWorth':b }, ignore_index = True)
print (all_data)
netWorth artist
0 5 a
1 3 b
2 10 d

Dataframe Object is not callable

When I run it, it keeps telling me the dataframe object is not callable.
class OptionDataWebGleaner():
def __init__(self):
ticker = pd.read_csv('Yahoo_ticker_List.csv')['AUB.AX'].values
stock = raw_input('Please give the ticker of your selected option?\n')
if stock in ticker:
self.stock = stock
else:
raise TypeError('Your option is not available here.')
date_norm = raw_input('Please give your maturity date in the format of mm/dd/yyyy\n')
maturity_date = datetime.strptime(date_norm, '%m/%d/%Y').date()
self.maturity_date = maturity_date
self.today = date.today()
dates = ['1481846400', '1484870400', '1487289600']
maturity_dates = [date(2016, 12, 16), date(2017, 1, 20), date(2017, 2, 17)]
date_dict = {}
for v in zip(dates, maturity_dates):
date_dict[v[1]] = v[0]
try:
self.d = date_dict[self.maturity_date]
except:
print('Your maturuity date is not available')
option = raw_input('Please give the type of your option, either call or put\n')
self.option_type = option + 's'
#property
def crawl_data(self): # self #option_type: calls or puts. str
stock = self.stock
option_type = self.option_type
maturity_date = self.maturity_date
d = self.d
chromedriver = "/Users/Miya/Downloads/chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
today = self.today
## Get the url
url = 'http://finance.yahoo.com/quote/' + stock + '/options?date=' + d
## Crawl data
driver.get(url)
html_source = driver.page_source
## Beautifulsoup
soup = BeautifulSoup(html_source, 'html.parser')
if soup.find('table', option_type) is not None:
stock_price = [float(i.text) for i in soup.findAll('span', 'Fz(36px)')]
title = [i.text for i in soup.find('table', option_type).find_all('th')]
text = [i.text for i in soup.find('table', option_type).find_all('td')]
rows = [row for row in soup.find('table', option_type).find_all("tr")]
l_table = len(rows) - 1
## call/put data
dictionary = {}
dictionary['maturity_date'] = [maturity_date] * l_table
dictionary['date'] = [today] * l_table
dictionary['stock_price'] = stock_price * l_table
for j in range(10):
key = title[j]
dictionary[key] = []
for i in range(l_table):
dictionary[key].append(text[10 * i + j])
## write into dataframe
dataframe = pd.DataFrame(dictionary)
return dataframe
def clean_data(self):
dataframe = self.crawl_data()
print('Remove unexpected symbols...')
columns_to_set = ['Last Price', 'Open Interest', 'Strike', 'Volume', 'Implied Volatility']
for i in columns_to_set:
series = dataframe[i]
series_new = []
for j in series:
j = str(j)
j_new = ''.join(ch for ch in j if (ch != '%') and (ch != ','))
series_new.append(j_new)
dataframe[i] = series_new
print('Change the data type...')
## change the dtype
columns_to_change = ['Last Price', 'Open Interest', 'Strike', 'Volume', 'stock_price', 'Implied Volatility']
for i in columns_to_change:
dataframe_cleaned[i] = dataframe[i].astype(float)
print("Remove missing values...")
dataframe_cleaned = dataframe_cleaned.dropna()
# print("Clean Outliers...")
# dataframe = dataframe.loc[dataframe['Implied Volatility'] <= 2]
return dataframe_cleaned
def save_file(self):
save_file = raw_input("Do you want to save the file into csv? Type Y for yes, N or no\n ")
d = self.d
stock = self.stock
df_option = self.clean_data()
if save_file == 'Y':
csv_name = stock + d + '.csv'
df_option.to_csv(csv_name)
print("File Saved!")
def viz(self):
dataframe = self.clean_data()
stock = self.stock
time_to_maturity = []
dataframe = dataframe.sort_values(by='Strike')
## grab dataframe, then relevant data
for i, j in zip(dataframe.maturity_date, dataframe.date):
time_to_maturity.append((i - j).days / 365)
strike_price = dataframe['Strike']
# generate pseudo-implied volatility by using strike price and time-to-maturity as parameters
implied_vol = dataframe['Implied Volatility'].values
strike_price, time_to_maturity = np.meshgrid(strike_price, time_to_maturity)
fig = plot.figure(figsize=(10, 5)) ## a plot object
ax = Axes3D(fig) # create a 3D object/handle
##plot surface: array row/column stride(step size:2)
##plot surface: array row/column stride(step size:2)
surf = ax.plot_surface(strike_price, time_to_maturity, implied_vol, rstride=2, cstride=2, cmap=cm.coolwarm,
linewidth=0.5, antialiased=False)
# set x,y,a labels
ax.set_xlabel('Strike Price')
ax.set_ylabel('time to maturity')
ax.set_zlabel('implied volatility%')
plot.suptitle(stock)
plot.show()
def summary(self):
dataframe = self.clean_data
print(dataframe.describe())
OptionDataWebGleaner().viz()
The problem is the property decorator on crawl_data. This answer explains how the property decorator actually works, but basically, dataframe.crawl_data is the dataframe returned by the function, not the function. So dataframe.crawl_data() in the first line of clean_data is trying to call the dataframe, not the function.
Here's an example:
>>> class Test(object):
... #property
... def example(self):
... return 1
...
>>> t = Test()
>>> t.example
1
>>> t.example()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
This question really could have done with the stacktrace. It would have lead us right to line with the problematic call.

Python json dictionary to array

I am having some trouble understanding json dictionary and array. I have a script that is scraping information from a website.
models.txt is just a list of model numbers such as
30373
30374
30375
and json_descriptions.txt is a list of the keys I want
sku
price
listprice
issoldout
The code is:
import urllib
import re
import json
modelslist = open("models.txt").read()
modelslist = modelslist.split("\n")
descriptionlist = open("json_descriptions.txt").read()
descriptionlist = descriptionlist.split("\n")
for model in modelslist:
htmltext = urllib.urlopen("http://dx.com/p/GetProductInfoRealTime?skus="+model)
htmltext = json.load(htmltext)
if htmltext['success'] == True:
def get_data(dict_index, key):
return htmltext[u"data"][dict_index][key]
for description in descriptionlist:
info = description, (get_data(0,description))
print info
else:
print "product does not exist"
If I print out info I get:
sku 30373
price 9.10
listprice 17.62
issoldout False
so that means info[0] is:
sku
price
listprice
issoldout
and info[1] is:
30373
9.10
17.62
False
I would like to know if there is a way that I can have this:
loop 1 = ['sku','30373','price','4.90','listprice','0','issoldout','False']
loop 2 = ['sku','30374','price','10.50','listprice','0','issoldout','False']
info[0] = sku info[1] = 30373 info[2] = price info[3] = 4.90 info[4] = listprice info[5] = 0 info[6] = issoldout info[7] = False and then repeat that with a new list for the next loop through.
I have tried using info = json.dumps(info) but that just gives info[0] = [[[[ and info[1] = """" info[2] = spli and so on
Like this?
info = []
for model in modelslist:
htmltext = urllib.urlopen("http://dx.com/p/GetProductInfoRealTime?skus="+model)
htmltext = json.load(htmltext)
if htmltext['success'] == True:
def get_data(dict_index, key):
return htmltext[u"data"][dict_index][key]
for description in descriptionlist:
info.append(description)
info.append(get_data(0,description))
print info

Categories