Skip to main content
edited body; edited tags
Source Link
toolic
  • 15.8k
  • 6
  • 29
  • 217

I have a program that lists and downloads a year of history of two stock tickers (AMZN and GOOG). I download these to a SQL database, C:\Databases\hist_prices.db, inside a table called hist_prices_tickers.

How can I organize my code for a more Pythonic approach?

I intend to use this SQL database for a machine learning-learning project later on. Is this method of calling the database the best way possible?

import yfinance as yf
import pandas as pd
import sqlite3, shutil, glob, os
from pathlib import Path

NewFolderPath  = r"C:\Databases\\"
isFile = os.path.exists(NewFolderPath)
if isFile is False:
    Path(NewFolderPath).mkdir(parents=True, exist_ok=True)

df = pd.DataFrame()
df2 = pd.DataFrame()
tickers = ["AMZN","GOOG"]

#SQL Connect
conn = sqlite3.connect(NewFolderPath+'hist_prices.db')
c = conn.cursor()
dest = sqlite3.connect(':memory:')
conn.backup(dest)
conn.commit()

for ticker in tickers:
    tkr = yf.Ticker(ticker)
    hist = tkr.history(period="1y")
    df2 = df2.append(hist)
    df2['Ticker'] = ticker
    df = df.append(df2.reset_index())

    df.fillna(0, inplace=True)
    df = df.applymap(str)
    df = df.apply(lambda x: x.str.replace('.', ','))
    df['Date'] = df['Date'].apply(pd.to_datetime)
    df.to_sql('hist_prices_tickers', conn, index=False, if_exists='append')

df = pd.read_sql_query("SELECT * from hist_prices_tickers", conn)
df.drop_duplicates(subset=None, keep="last", inplace=True)
df.to_sql('hist_prices_tickers', conn,  if_exists='replace', index=False)

print(df)

conn.close()

I have a program that lists and downloads a year of history of two stock tickers (AMZN and GOOG). I download these to a SQL database, C:\Databases\hist_prices.db, inside a table called hist_prices_tickers.

How can I organize my code for a more Pythonic approach?

I intend to use this SQL database for a machine learning project later on. Is this method of calling the database the best way possible?

import yfinance as yf
import pandas as pd
import sqlite3, shutil, glob, os
from pathlib import Path

NewFolderPath  = r"C:\Databases\\"
isFile = os.path.exists(NewFolderPath)
if isFile is False:
    Path(NewFolderPath).mkdir(parents=True, exist_ok=True)

df = pd.DataFrame()
df2 = pd.DataFrame()
tickers = ["AMZN","GOOG"]

#SQL Connect
conn = sqlite3.connect(NewFolderPath+'hist_prices.db')
c = conn.cursor()
dest = sqlite3.connect(':memory:')
conn.backup(dest)
conn.commit()

for ticker in tickers:
    tkr = yf.Ticker(ticker)
    hist = tkr.history(period="1y")
    df2 = df2.append(hist)
    df2['Ticker'] = ticker
    df = df.append(df2.reset_index())

    df.fillna(0, inplace=True)
    df = df.applymap(str)
    df = df.apply(lambda x: x.str.replace('.', ','))
    df['Date'] = df['Date'].apply(pd.to_datetime)
    df.to_sql('hist_prices_tickers', conn, index=False, if_exists='append')

df = pd.read_sql_query("SELECT * from hist_prices_tickers", conn)
df.drop_duplicates(subset=None, keep="last", inplace=True)
df.to_sql('hist_prices_tickers', conn,  if_exists='replace', index=False)

print(df)

conn.close()

I have a program that lists and downloads a year of history of two stock tickers (AMZN and GOOG). I download these to a SQL database, C:\Databases\hist_prices.db, inside a table called hist_prices_tickers.

How can I organize my code for a more Pythonic approach?

I intend to use this SQL database for a machine-learning project later on. Is this method of calling the database the best way possible?

import yfinance as yf
import pandas as pd
import sqlite3, shutil, glob, os
from pathlib import Path

NewFolderPath  = r"C:\Databases\\"
isFile = os.path.exists(NewFolderPath)
if isFile is False:
    Path(NewFolderPath).mkdir(parents=True, exist_ok=True)

df = pd.DataFrame()
df2 = pd.DataFrame()
tickers = ["AMZN","GOOG"]

#SQL Connect
conn = sqlite3.connect(NewFolderPath+'hist_prices.db')
c = conn.cursor()
dest = sqlite3.connect(':memory:')
conn.backup(dest)
conn.commit()

for ticker in tickers:
    tkr = yf.Ticker(ticker)
    hist = tkr.history(period="1y")
    df2 = df2.append(hist)
    df2['Ticker'] = ticker
    df = df.append(df2.reset_index())

    df.fillna(0, inplace=True)
    df = df.applymap(str)
    df = df.apply(lambda x: x.str.replace('.', ','))
    df['Date'] = df['Date'].apply(pd.to_datetime)
    df.to_sql('hist_prices_tickers', conn, index=False, if_exists='append')

df = pd.read_sql_query("SELECT * from hist_prices_tickers", conn)
df.drop_duplicates(subset=None, keep="last", inplace=True)
df.to_sql('hist_prices_tickers', conn,  if_exists='replace', index=False)

print(df)

conn.close()
added 141 characters in body
Source Link

I have a program that lists and downloads a year of history of two stock tickers (AMZN and GOOG). I download these to a SQL database, C:\Databases\hist_prices.db, inside a table called hist_prices_tickers.

How can I organize my code for a more Pythonic approach?

I intend to use this SQL database for a machine learning project later on. Is this method of calling the database the best way possible?

import yfinance as yf
import pandas as pd
import sqlite3, shutil, glob, os
from pathlib import Path

NewFolderPath  = r"C:\Databases\\"
isFile = os.path.exists(NewFolderPath)
if isFile is False:
    Path(NewFolderPath).mkdir(parents=True, exist_ok=True)

df = pd.DataFrame()
df2 = pd.DataFrame()
tickers = ["AMZN","GOOG"]

#SQL Connect
conn = sqlite3.connect(NewFolderPath+'hist_prices.db')
c = conn.cursor()
dest = sqlite3.connect(':memory:')
conn.backup(dest)
conn.commit()

for ticker in tickers:
    tkr = yf.Ticker(ticker)
    hist = tkr.history(period="1y")
    df2 = df2.append(hist)
    df2['Ticker'] = ticker
    df = df.append(df2.reset_index())

    df.fillna(0, inplace=True)
    df = df.applymap(str)
    df = df.apply(lambda x: x.str.replace('.', ','))
    df['Date'] = df['Date'].apply(pd.to_datetime)
    df.to_sql('hist_prices_tickers', conn, index=False, if_exists='append')

df = pd.read_sql_query("SELECT * from hist_prices_tickers", conn)
df.drop_duplicates(subset=None, keep="last", inplace=True)
df.to_sql('hist_prices_tickers', conn,  if_exists='replace', index=False)

print(df)

conn.close()

I have a program that lists and downloads a year of history of two stock tickers (AMZN and GOOG). I download these to a SQL database, C:\Databases\hist_prices.db, inside a table called hist_prices_tickers.

How can I organize my code for a more Pythonic approach?

import yfinance as yf
import pandas as pd
import sqlite3, shutil, glob, os
from pathlib import Path

NewFolderPath  = r"C:\Databases\\"
isFile = os.path.exists(NewFolderPath)
if isFile is False:
    Path(NewFolderPath).mkdir(parents=True, exist_ok=True)

df = pd.DataFrame()
df2 = pd.DataFrame()
tickers = ["AMZN","GOOG"]

#SQL Connect
conn = sqlite3.connect(NewFolderPath+'hist_prices.db')
c = conn.cursor()
dest = sqlite3.connect(':memory:')
conn.backup(dest)
conn.commit()

for ticker in tickers:
    tkr = yf.Ticker(ticker)
    hist = tkr.history(period="1y")
    df2 = df2.append(hist)
    df2['Ticker'] = ticker
    df = df.append(df2.reset_index())

    df.fillna(0, inplace=True)
    df = df.applymap(str)
    df = df.apply(lambda x: x.str.replace('.', ','))
    df['Date'] = df['Date'].apply(pd.to_datetime)
    df.to_sql('hist_prices_tickers', conn, index=False, if_exists='append')

df = pd.read_sql_query("SELECT * from hist_prices_tickers", conn)
df.drop_duplicates(subset=None, keep="last", inplace=True)
df.to_sql('hist_prices_tickers', conn,  if_exists='replace', index=False)

print(df)

conn.close()

I have a program that lists and downloads a year of history of two stock tickers (AMZN and GOOG). I download these to a SQL database, C:\Databases\hist_prices.db, inside a table called hist_prices_tickers.

How can I organize my code for a more Pythonic approach?

I intend to use this SQL database for a machine learning project later on. Is this method of calling the database the best way possible?

import yfinance as yf
import pandas as pd
import sqlite3, shutil, glob, os
from pathlib import Path

NewFolderPath  = r"C:\Databases\\"
isFile = os.path.exists(NewFolderPath)
if isFile is False:
    Path(NewFolderPath).mkdir(parents=True, exist_ok=True)

df = pd.DataFrame()
df2 = pd.DataFrame()
tickers = ["AMZN","GOOG"]

#SQL Connect
conn = sqlite3.connect(NewFolderPath+'hist_prices.db')
c = conn.cursor()
dest = sqlite3.connect(':memory:')
conn.backup(dest)
conn.commit()

for ticker in tickers:
    tkr = yf.Ticker(ticker)
    hist = tkr.history(period="1y")
    df2 = df2.append(hist)
    df2['Ticker'] = ticker
    df = df.append(df2.reset_index())

    df.fillna(0, inplace=True)
    df = df.applymap(str)
    df = df.apply(lambda x: x.str.replace('.', ','))
    df['Date'] = df['Date'].apply(pd.to_datetime)
    df.to_sql('hist_prices_tickers', conn, index=False, if_exists='append')

df = pd.read_sql_query("SELECT * from hist_prices_tickers", conn)
df.drop_duplicates(subset=None, keep="last", inplace=True)
df.to_sql('hist_prices_tickers', conn,  if_exists='replace', index=False)

print(df)

conn.close()
deleted 54 characters in body; edited title
Source Link
Peilonrayz
  • 44.6k
  • 7
  • 80
  • 158

How to better organize the code for stock prices Stock price history SQL database with Python, Pandas and yfinance

What this code does: CreatesI have a folder named Databases in C: path,program that lists twoand downloads a year of history of two stock tickers (AMZN and GOOG),creates an sql .db file, iterates throgh the tickers and add their 1y history I download these to thea SQL database called hist_prices, C:\Databases\hist_prices.db, inside a table called hist_prices_tickershist_prices_tickers.

How can iI organize my code for a more pythonicPythonic approach?

import yfinance as yf
import pandas as pd
import sqlite3, shutil, glob, os
from pathlib import Path

NewFolderPath  = r"C:\Databases\\"
isFile = os.path.exists(NewFolderPath)
if isFile is False:
    Path(NewFolderPath).mkdir(parents=True, exist_ok=True)

df = pd.DataFrame()
df2 = pd.DataFrame()
tickers = ["AMZN","GOOG"]

#SQL Connect
conn = sqlite3.connect(NewFolderPath+'hist_prices.db')
c = conn.cursor()
dest = sqlite3.connect(':memory:')
conn.backup(dest)
conn.commit()

for ticker in tickers:
    tkr = yf.Ticker(ticker)
    hist = tkr.history(period="1y")
    df2 = df2.append(hist)
    df2['Ticker'] = ticker
    df = df.append(df2.reset_index())

    df.fillna(0, inplace=True)
    df = df.applymap(str)
    df = df.apply(lambda x: x.str.replace('.', ','))
    df['Date'] = df['Date'].apply(pd.to_datetime)
    df.to_sql('hist_prices_tickers', conn, index=False, if_exists='append')

df = pd.read_sql_query("SELECT * from hist_prices_tickers", conn)
df.drop_duplicates(subset=None, keep="last", inplace=True)
df.to_sql('hist_prices_tickers', conn,  if_exists='replace', index=False)

print(df)

conn.close()

How to better organize the code for stock prices history SQL database with Python, Pandas and yfinance

What this code does: Creates a folder named Databases in C: path, lists two stock tickers (AMZN and GOOG),creates an sql .db file, iterates throgh the tickers and add their 1y history to the database called hist_prices inside a table called hist_prices_tickers.

How can i organize my code for a more pythonic approach?

import yfinance as yf
import pandas as pd
import sqlite3, shutil, glob, os
from pathlib import Path

NewFolderPath  = r"C:\Databases\\"
isFile = os.path.exists(NewFolderPath)
if isFile is False:
    Path(NewFolderPath).mkdir(parents=True, exist_ok=True)

df = pd.DataFrame()
df2 = pd.DataFrame()
tickers = ["AMZN","GOOG"]

#SQL Connect
conn = sqlite3.connect(NewFolderPath+'hist_prices.db')
c = conn.cursor()
dest = sqlite3.connect(':memory:')
conn.backup(dest)
conn.commit()

for ticker in tickers:
    tkr = yf.Ticker(ticker)
    hist = tkr.history(period="1y")
    df2 = df2.append(hist)
    df2['Ticker'] = ticker
    df = df.append(df2.reset_index())

    df.fillna(0, inplace=True)
    df = df.applymap(str)
    df = df.apply(lambda x: x.str.replace('.', ','))
    df['Date'] = df['Date'].apply(pd.to_datetime)
    df.to_sql('hist_prices_tickers', conn, index=False, if_exists='append')

df = pd.read_sql_query("SELECT * from hist_prices_tickers", conn)
df.drop_duplicates(subset=None, keep="last", inplace=True)
df.to_sql('hist_prices_tickers', conn,  if_exists='replace', index=False)

print(df)

conn.close()

Stock price history SQL database with Python, Pandas and yfinance

I have a program that lists and downloads a year of history of two stock tickers (AMZN and GOOG). I download these to a SQL database, C:\Databases\hist_prices.db, inside a table called hist_prices_tickers.

How can I organize my code for a more Pythonic approach?

import yfinance as yf
import pandas as pd
import sqlite3, shutil, glob, os
from pathlib import Path

NewFolderPath  = r"C:\Databases\\"
isFile = os.path.exists(NewFolderPath)
if isFile is False:
    Path(NewFolderPath).mkdir(parents=True, exist_ok=True)

df = pd.DataFrame()
df2 = pd.DataFrame()
tickers = ["AMZN","GOOG"]

#SQL Connect
conn = sqlite3.connect(NewFolderPath+'hist_prices.db')
c = conn.cursor()
dest = sqlite3.connect(':memory:')
conn.backup(dest)
conn.commit()

for ticker in tickers:
    tkr = yf.Ticker(ticker)
    hist = tkr.history(period="1y")
    df2 = df2.append(hist)
    df2['Ticker'] = ticker
    df = df.append(df2.reset_index())

    df.fillna(0, inplace=True)
    df = df.applymap(str)
    df = df.apply(lambda x: x.str.replace('.', ','))
    df['Date'] = df['Date'].apply(pd.to_datetime)
    df.to_sql('hist_prices_tickers', conn, index=False, if_exists='append')

df = pd.read_sql_query("SELECT * from hist_prices_tickers", conn)
df.drop_duplicates(subset=None, keep="last", inplace=True)
df.to_sql('hist_prices_tickers', conn,  if_exists='replace', index=False)

print(df)

conn.close()
Source Link
Loading