Home > OS >  Formatting Column Names in pandas_datareader dataframes when downloading multiple ticker symbols at
Formatting Column Names in pandas_datareader dataframes when downloading multiple ticker symbols at

Time:08-25

Rather inconvenient is the way pandas_datareader formats dataframes when downloading multiple ticker symbols at once.

Index       0       1       2
Attributes  Close   Close   High        
Symbols     Ticker1 Ticker2 Ticker1 
"some date" 10      20      10.5           
"some date" 11      21      11.2     

          

How do I format this into something more usabale, that looks like a DF when only downlaoding a single ticker symbol? I believe the DF index should just be the dates. Combining Attributes and Symbols into one unified column name would be the way to go for me.

Date         Close_Ticker1 Close_Ticker2 High_Ticker1 
"some date"  10            20            10.5           
"some date"  11            21            11.2 

Following is the code that produces the actual dataframe that I have shown in general form above. Ticker symbols are just for example, in final code there will be many tickers in the list, therefore having an automated way to solve the problem rather than to manually define each column individually is definitely preffered:

import pandas_datareader as pdr

# define ticker symbols
ticker = ['^GSPC', '^VIX']

# fetch data
df = pdr.DataReader(ticker, 'yahoo', start='1990-01-01', end='today')

CodePudding user response:

You can try:

df = (df
 .set_axis(df.columns.map(lambda x: f'{x[1]}_{x[2]}'))
 .rename_axis('Date')
 .reset_index()
)

CodePudding user response:

Who knew it could be this easy. This works for me:

df.columns = df.columns.map('_'.join)
  • Related