Flatten a Multi-Index DataFrame
Resource
Whilst a multi-index datafame can be useful for complex data orginisation, for example across multiple markets and time periods. Flattening the data has it’s own advantages; making for easier data manipulation, merging with other datasets and visulising. Here is a quick example.
# Import
import pandas as pd
import yfinance as yf
# Fetch data
#S&P 500 Vanguard - VOO
#S&P 500 Black Rock - BSPIX
tickers = ['VOO','VTWO']
df = yf.download(tickers, start='2019-01-01', end='2024-10-11')
[*********************100%***********************] 2 of 2 completed
print(df.columns)
MultiIndex([('Adj Close', 'VOO'),
('Adj Close', 'VTWO'),
( 'Close', 'VOO'),
( 'Close', 'VTWO'),
( 'High', 'VOO'),
( 'High', 'VTWO'),
( 'Low', 'VOO'),
( 'Low', 'VTWO'),
( 'Open', 'VOO'),
( 'Open', 'VTWO'),
( 'Volume', 'VOO'),
( 'Volume', 'VTWO')],
names=['Price', 'Ticker'])
# Completely flatten the DataFrame
df_flattened = df.stack(future_stack=True).reset_index()
print(df_flattened.columns)
Index(['Date', 'Ticker', 'Adj Close', 'Close', 'High', 'Low', 'Open',
'Volume'],
dtype='object', name='Price')