1

I would like to read all the CSV files in my path and assign them each to a different variable name within my python code.
For example,

dshfd9438dks.csv
ansjewi38Ekd.csv

Would turn into:

df1 and df2

The code I have now is:

for f in glob.iglob('*.csv'): 
df = pd.read_csv(f, low_memory=False)

How would I manipulate it to add a number to the df var?

1
  • dictionary as stated is the way. Variables are meant to be a developers reflection with no automation in creation. Commented Dec 10, 2018 at 15:48

2 Answers 2

4

Use a dictionary for a variable number of variables

It's poor practice to name linked variables explicitly. In this case, you can use a dictionary with enumerate:

dfs = {}
for idx, f in enumerate(glob.iglob('*.csv'), 1):
    dfs[idx] = pd.read_csv(f, low_memory=False)

If you wish, you can convert this into a dictionary comprehension:

files = enumerate(glob.iglob('*.csv'), 1)
dfs = {idx: pd.read_csv(f, low_memory=False) for idx, f in files}

Then access your dataframes via dfs[1], dfs[2], etc.

Sign up to request clarification or add additional context in comments.

Comments

1

Why not put them in a dictionary?

dfs = dict(("df{}".format(i), pd.read_csv(f)) for i,f in enumerate(glob.iglob('*.csv')))

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.