artDeco
artDeco

Reputation: 520

Set up MultiIndex DataFrame from multiple CSV files in DateTime series

I have a list of time series price data in CSV format that is read as follows:

asxList = ['ANZ', 'NAB', 'WBC']

for asxCode in asxList:
    ohlcData = pd.DataFrame.from_csv(asxCode+'.CSV', header=0)

Example output:

enter image description here

How do I assemble all the ohlcData in particular order, firstly by DateTime index, and secondly by the asxList ['ANZ', 'NAB', 'WBC'] index, then followed by the data columns?

Upvotes: 3

Views: 1038

Answers (2)

BENY
BENY

Reputation: 323326

Almost same with Dyz, just using keys from concat

asxList = ['ANZ', 'NAB', 'WBC']
l=[]
for asxCode in asxList:
    l.append(pd.DataFrame.from_csv(asxCode+'.CSV', header=0))

pd.concat(l,keys=asxList)

Upvotes: 2

DYZ
DYZ

Reputation: 57085

Create a list of dataframes, add a code column to each dataframe:

dfs = []
for asxCode in asxList:
    df = pd.DataFrame.from_csv(asxCode+'.CSV', header=0)
    df['code'] = asxCode
    dfs.append(df)

Concatenate the dataframes, add the code column to the index:

pd.concat(dfs).reset_index().set_index(['index', 'code'])

Upvotes: 4

Related Questions