1

I want to load multiple CSV files into one dataframe. Each CSV contains stock data with 6 columns ( 'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume' ) . I managed to load the CSV files, but I'm missing the column name ( each ticker, from CSV ).

sp500 =  os.listdir(os.path.splitext(os.getcwd()+'/spy500')[0])

combined = pd.concat([pd.read_csv('spy500/'+i, parse_dates=True, index_col='Date') for i in sp500], axis=1)

output:

Open    | High  |Low    |Close| Adj Close   |Volume|    Open|   High|   Low Close|  Adj Close   |Volume

desire output:

AAPL                                            | GOOG                  
Open |High  |Low    |Close  |Adj Close  |Volume |Open   |High   |Low    |Close  |Adj Close  |Volume

the output is correct, the only thing I need is to add a multi level column: 5986 rows × 3030 columns

Fede
  • 1,542
  • 3
  • 22
  • 39

1 Answers1

1

Use dictionary comprehension:

comp = {i.split('.')[0]: 
        pd.read_csv('spy500/'+i, parse_dates=True, index_col='Date') for i in sp500}
combined = pd.concat(comp, axis=1)
jezrael
  • 729,927
  • 78
  • 1,141
  • 1,090