Hi,
I am just trying to familiar with .apply function to 2 columns but the output keep showing error. I know it will be easier to use .pct_change(), but I want to master the apply function.
It just a simple function to divide today's Adj Close by yesterday Adj Close.
def cc_return (data):
return data['Adj Close']/data['Adj close shift 1']
df['cc_return']= df.apply(cc_return)
The output error shown are as below:
KeyError: 'Adj Close shift 1'
Not sure what is the problem.
Please help. Thanks
Does you object df, have a column called - 'Adj close shift 1' ? try print(df.columns) to check ?
the columns 'Adj Close shift 1 is there, but it is not working.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import warnings
import yfinance as yf
warnings.filterwarnings('ignore')
ticker1= 'baba'
start1= '2020-1-1'
end1='2021-12-31'
df=yf.download(ticker1, start1, end1)
df['Adj Close shift 1']= df['Adj Close'].shift(1)
def cc_return(data):
data['cc return']= data['Adj Close']/ data['Adj Close shift 1']
return cc_return
df.apply(cc_return)
Hi Kheng,
One plausible reason for this can be some error in the column name you provided. You can try rechecking the same. You can also refer to the following link to know more about df.apply
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html
Please do let us know if the column names are correct and the issue still persists.
Thanks,
Akshay
I think the column is fine, still figuring. I attached the whole code below, please feel free to test.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import warnings
import yfinance as yf
warnings.filterwarnings('ignore')
ticker1= 'baba'
start1= '2020-1-1'
end1='2021-12-31'
df=yf.download(ticker1, start1, end1)
df['Adj Close shift 1']= df['Adj Close'].shift(1)
def cc_return(data):
data['cc return']= data['Adj Close']/ data['Adj Close shift 1']
return cc_return
df.apply(cc_return)
Hello Kheng,
It's good to know you are trying to learn that function. It's actually very useful. Let's find out what happened to you.
If you run your code as it is: The last error message is:
" KeyError: 'Adj Close' "
This actually happens because the function is trying to find a row named 'Adj Close'. However, you have a 'column' named like that, right? So in order to have the function applied to columns and not to rows, you have to specify the apply function like this:
" df.apply(cc_return, axis=1) "
In that way you assure it is well applied.
Now if we make the above correction and run again the code, we have another error. If we try to print the returns, see what we have as output:
""""""""""""""""""""""""""""""""""""
Date
2020-01-02 <function cc_return at 0x000002CF9A6EDEE0>
2020-01-03 <function cc_return at 0x000002CF9A6EDEE0>
2020-01-06 <function cc_return at 0x000002CF9A6EDEE0>
2020-01-07 <function cc_return at 0x000002CF9A6EDEE0>
2020-01-08 <function cc_return at 0x000002CF9A6EDEE0>
2021-12-23 <function cc_return at 0x000002CF9A6EDEE0>
2021-12-27 <function cc_return at 0x000002CF9A6EDEE0>
2021-12-28 <function cc_return at 0x000002CF9A6EDEE0>
2021-12-29 <function cc_return at 0x000002CF9A6EDEE0>
2021-12-30 <function cc_return at 0x000002CF9A6EDEE0>
Length: 504, dtype: object
""""""""""""""""""""""""""""""""""
As you could see, the returns are not returns, each row is actually a function object. This happens because in your defined function you return "cc_return". You are doing something like a recursive function. In order to avoid that, you should write your function as the following lines:
""""""""""""""""""""""""""""""""""
def cc_return(data):
data['cc return'] = data['Adj Close']/ data['Adj Close shift 1']
return data['cc return']
""""""""""""""""""""""""""""""""""""
These two corrections will make you run your code smoothly,
I hope everything's clear, in case you have more doubts, please let us know!
Thanks
José Carlos