79172953

Date: 2024-11-09 14:18:18
Score: 0.5
Natty:
Report link

When you drop columns, pandas doesn't automatically clean up unused levels in the MultiIndex. The levels still contain all original values even if they're no longer used.

If you print the df.columns you can see that the requested "AAPL" has been removed. To update the FrozenList that the df.columns.levels returns you will need to remove the unused levels.

tickers = ['AAPL', 'TSLA', 'AMZN', 'GOOGL', 'MSFT', 'META', 'NVDA', 'PYPL', 'ADBE', 'NFLX']

data = yf.download(tickers, period="1y", interval="1wk", group_by='ticker')

# I have changed the code here for readability.
data = data.drop(columns="AAPL", axis=1, level=0)

data.columns = data.columns.remove_unused_levels()

pandas.MultiIndex.remove_unused_levels

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Starts with a question (0.5): When you
  • Low reputation (1):
Posted by: p13kara3