Python Pandas update dataframe if ticker has traded

30 Views Asked by At

I have several dataframes which can be accessed with dataframes[0] then [1] so forth which contain the history of individual stock tickers with the Date,Ticker,Open,High,Low,Close,Volume columns.(Ticker column has ticker name. Example TSLA.

Then I have daily dataframes which can be accessed with daily_dataframes[0] then [1] so forth which contain daily prices of all the above stock tickers.Ticker column has ticker names. Example TSLA,MSFT,APPL etc

I want to check the dailydatframe and if the ticker has traded then to update the relevant historical ticker dataframe with the daily data.

How can I do this. All the help will be greatly appreciated

Tried multi loop through data sets and get the ticker names from the history and daily. Need to compare the ticker then update the data which is where I am stuck ......................................

import pandas as pd
import glob
from pathlib import Path
import re
from datetime import datetime

# Read All files from the CSV Folder History and Daily

path = 'C:/Users/im/Desktop/Technical Analysis/New folder/'
path_daily = 'C:/Users/im/Desktop/Technical Analysis/Daily Summ/'



# Requesting a list of all the csv files present
# in  specified folder

files = glob.glob(path+"*.csv")
files_daily=glob.glob(path_daily+"*.csv")


# assign path

file_count = len(files)
file_count_daily = len(files_daily)

# create empty list

dataframes_list = []
dataframes_list_daily = []


# Create datasets for each Tickers history

for i in range(file_count):
temp_df = pd.read_csv(files[i],skipinitialspace  =True,parse_dates=True)
symbol=(Path(files[i]).stem)  #Get Symbol from file path
temp_df.drop(columns=['TradeVolume', 'Turnover (Rs.)'],inplace=True)


temp_df.columns = ['Date', 'Open', 'High','Low','Close','ShareVolume']
temp_df['Symbol'] = symbol

# shift column 'Symbol' to 2nd  position

sixth_column = temp_df.pop('Symbol')


# insert column using insert(position,column_name,
# first_column) function

temp_df.insert(1, 'Symbol', sixth_column)

temp_df['Date'] = pd.to_datetime(temp_df["Date"],dayfirst=True)
temp_df['Date'].dt.strftime('%d-%m-%y')
dataframes_list.append(temp_df)



# Create  datasets for each day 

for i in range(file_count_daily):
    temp_df1 = pd.read_csv(files_daily[i],skipinitialspace = True)
    daily_sum_path=(Path(files_daily[i]).stem)
    p = re.compile('(\s*)trade-summary(\s*)')
    date=p.sub('', daily_sum_path)
    temp_df1.drop(["Company Name", "Trade Volume",'Previous Close   (Rs.)','Change(Rs)','Change (%)'],    axis = 1, inplace=True)


temp_df1.columns = ['Symbol','ShareVolume', 'Open', 'High','Low','Close',]
temp_df1.Symbol = temp_df1.Symbol.apply(lambda x : x.replace('.',''))
temp_df1['Date'] = date
temp_df1.Date = temp_df1.Date.apply(lambda x : x.replace('.','-'))
temp_df1['Date'] = pd.to_datetime(temp_df1["Date"],dayfirst=True)
temp_df1['Date'].dt.strftime('%d-%m-%y')
# shift column 'Date' to first position
first_column = temp_df1.pop('Date')

# insert column using insert(position,column_name,
# first_column) function

temp_df1.insert(0, 'Date', first_column)

# shift column 'SharVolume' to Last position

last_column = temp_df1.pop('ShareVolume')

# insert column using insert(position,column_name,
# last_column) function

temp_df1.insert(6, 'ShareVolume', last_column)

dataframes_list_daily.append(temp_df1)

......................................

0

There are 0 best solutions below