I want to drop a column level and columns to the right, from data downloaded from yahoo finance.
FAANG = yf.download(['AAPL','GOOGL','NFLX','META','AMZN','SPY'],
start = '2008-01-01',end = '2022-12-31')
FAANG_AC = FAANG.drop(FAANG.columns[6:36],axis=1)
FAC = FAANG_AC.droplevel(0,axis=1)
How do I combine .drop
and .droplevel
into a single variable, so that I do not have to use multiple variables in this situation?
CodePudding user response:
You don't need to use intermediate variables. You can chain everything:
FAANG = (yf.download(['AAPL','GOOGL','NFLX','META','AMZN','SPY'],
start='2008-01-01', end = '2022-12-31')
.drop(FAANG.columns[6:36], axis=1)
.droplevel(0, axis=1)
)
CodePudding user response:
You can add inplace=True
as a parameter for when calling those methods. Like:
FAANG.drop(FAANG.columns[6:36],axis=1, inplace=True)
Careful: it will modify the FAANG
variable.
Reference: https://www.askpython.com/python-modules/pandas/inplace-true-parameter