5

Is there a way to read multiple csv files into Pandas through a loop and define them as such?

for i in ['a', 'b', 'c', 'd']:
    csv_(i) = pd.read_csv('C:/test_{}.csv'.format(i))

I see multiple questions about reading and appending multiple csvs into a single dataframe. Not the other way around.

1 Answer 1

8

You can use dict comprehension for dict of DataFrames:

dfs = {i: pd.read_csv('C:/test_{}.csv'.format(i)) for i in ['a', 'b', 'c', 'd']}

print (dfs['a'])
Sign up to request clarification or add additional context in comments.

4 Comments

Oh wonderful! Initially I dismissed the idea but after playing with it a little all I had to do was add each dict/DataFrames into a list to perform multiple merge.
Thank you, If need one big dataframe, maybe help this
Much appreciated!

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.