I need to add new columns called 'Volume % Change', and "Close % Change" for this dataframe in PYTHON. The data is from yahoo finance, so it's always changable depends on the days entered. the syntax is below: import pandas_datareader as pdr import datetime import pandas as pd # Allow the full width of the data frame to show. pd.set_option('display.max_columns', None) pd.set_option('display.width', 1000) def getStock(stk): # Set and show dates. dt = datetime.date.today() dtPast = dt + datetime.timedelta(days=-5) print(dt) print(dtPast) # Call Yahoo finance to get stock data for the stock provided. df = pdr.get_data_yahoo(stk, start= datetime.datetime(dtPast.year, dtPast.month, dtPast.day), end = datetime.datetime(dt.year, dt.month, dt.day)) # Return a dataframe containing stock data to the calling instruction. return df dfMSFT = getStock("MSFT") print(dfMSFT) -------------------------------------------------------------------------- The formula for Volume % Change is (Current day volume-previous day volume)/previous day volume The formula for Close % Change is (Current day Close - previous day)/previous day Close The output is like the attached image. Please assist! Thanks
I need to add new columns called 'Volume % Change', and "Close % Change" for this dataframe in PYTHON.
The data is from yahoo finance, so it's always changable depends on the days entered.
the syntax is below:
import pandas_datareader as pdr
import datetime
import pandas as pd
# Allow the full width of the data frame to show.
pd.set_option('display.max_columns', None)
pd.set_option('display.width', 1000)
def getStock(stk):
# Set and show dates.
dt = datetime.date.today()
dtPast = dt + datetime.timedelta(days=-5)
print(dt)
print(dtPast)
# Call Yahoo finance to get stock data for the stock provided.
df = pdr.get_data_yahoo(stk,
start= datetime.datetime(dtPast.year, dtPast.month, dtPast.day),
end = datetime.datetime(dt.year, dt.month, dt.day))
# Return a dataframe containing stock data to the calling instruction.
return df
dfMSFT = getStock("MSFT")
print(dfMSFT)
--------------------------------------------------------------------------
The formula for Volume % Change is (Current day volume-previous day volume)/previous day volume
The formula for Close % Change is (Current day Close - previous day)/previous day Close
The output is like the attached image.
Please assist!
Thanks
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 3 images