2

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?

2 Answers 2

3

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)
        )
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.