Home Data Engineering Data DIY How To Efficiently Merge Stock Data in Python

How To Efficiently Merge Stock Data in Python

Audio version of the article

The Adj. Close Prices of ten stocks merged in a single DataFrame

#The historical stock prices saved in adj_prices are passed to 
#reduce()to apply the merge function recursively from left to right

reduce(lambda x, y: pd.merge(x, y, left_index = True, right_index =
True, how=’outer’), adj_prices)
reduce(function, iterable [, initializer])

A Little Bit Of Theory

import functools
# Imports the full module and then use functools.reduce()

from functools import reduce
# Only imports reduce() from functools to use it directly
from functools import reduce

numbers = [7,2,5,9]def

 return x * y

reduce(simple_prod, numbers)

from functools import reduce

numbers = [7,2,5,9]

reduce(lambda x,y: x * y, numbers)


Put Theory Into Practice

import pandas as pd
import yfinance as yf
from functools import reduce

# Changes numeric fields format to include 2 digits only
pd.set_option('display.float_format', '{:.2f}'.format) 

tickers = ['AAPL', 'AMZN', 'BLK' ,'CSCO', 'FB', 'MSFT', 'NFLX', 'T', 'UBER' ,'ZM']

adj_prices = []
for i in range(len(tickers)):
    close_price = pd.DataFrame(yf.download(tickers[i])['Adj Close'].dropna(axis=0, how='any'))
    close_price = close_price.loc[~close_price.index.duplicated(keep='last')]
    close_price.columns = [tickers[i]]

df = reduce(lambda x, y: pd.merge(x, y, left_index = True, 
                                  right_index = True ,how='outer'), adj_prices)
df.sort_index(ascending = False, inplace = True)
df.index = pd.to_datetime(df.index ).date


Example of yf.download() function applied on AAPL.

Single groups of prices appended to the adj_prices list


This article has been published from the source link without modifications to the text. Only the headline has been changed.

Source link

- Advertisment -

Most Popular

Introductory Guide on XCFramework and Swift Package

In WWDC 2019, Apple announced a brand new feature for Xcode 11; the capability to create a new kind of binary frameworks with a special format...

Understanding Self Service Data Management

https://dts.podtrac.com/redirect.mp3/www.dataengineeringpodcast.com/podlove/file/704/s/webplayer/c/episode/Episode-159-Isima.mp3 Summary The core mission of data engineers is to provide the business with a way to ask and answer questions of their data. This often...

Understanding Machine Learning Data Preparation Techniques

Predictive modeling machine learning projects, such as classification and regression, always involve some form of data preparation. The specific data preparation required for a dataset...

Java and Python in Top List of Self taught Languages

Here's a report for the times: Specops Software sifted data from Ahrefs.com using its Google and YouTube search analytics tool to surface a list of the programming languages people most...

Crypto bulls predict the future for Bitcoin

Bitcoin is back. The cryptocurrency last week passed the $18,000 level for the first time since its all-time peak in December 2017. As...

Tracking Machine Learning experiments with Allegro AI

https://cdn.changelog.com/uploads/practicalai/97/practical-ai-97.mp3 DevOps for deep learning is well… different. You need to track both data and code, and you need to run multiple different versions of...
- Advertisment -