2

I create multiple dataframes by for loop and concat them, which works fine. But I need to include loop variable as index. I cant find a way to set loop variable as index

maindf=pd.DataFrame()
for i in ['20170724','20170725','20170726']:
    a=pd.read_csv("somecsv."+str(i))
    maindf = pd.concat(maindf,a,axis=0)

Expected OP for maindf:

         A  B  C
20170724 1  2  3
         4  5  6
         7  8  9
20170725 11 22 33
         44 55 66
         77 88 99
20170725 111 222 333
         444 555 666
         777 888 999

2 Answers 2

3

I think you need append all DataFrames to list and then use concat with parameter keys, also for remove second level add reset_index with parameter drop:

dfs = []
vals = ['20170724','20170725','20170726']
for i in vals:
    a=pd.read_csv("somecsv."+str(i))
    dfs.append(a)
maindf = pd.concat(dfs,keys=vals).reset_index(level=1, drop=True)
Sign up to request clarification or add additional context in comments.

Comments

1

You can use pd.concat on a dictionary.

lst = ['20170724','20170725','20170726']
pd.concat({k: pd.read_csv('somecsv.{}'.format(i)) for k in lst})

Comments

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.