3

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?

2
  • What do you mean by "assemble": sort or group? Commented May 4, 2018 at 0:34
  • 1
    You may use concat and set a multilevel index, first Date then your tick Commented May 4, 2018 at 0:39

2 Answers 2

4

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'])
Sign up to request clarification or add additional context in comments.

Comments

2

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)

2 Comments

You can do pd.concat([pd.DataFrame.from_csv(asxCode+'.CSV', header=0) for asxCode in asxList], keys=asxList).
Thanks Wen and the single-line code by DyZ is very Pythonic!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.