简体   繁体   中英

Pandas read_csv multiple files

What's the best way to loop through a bunch of files and create separate data frames for each file? I've looked through other questions, but it seems the point in each of those is to concatenate files into one data frame.

For example, if I have mylist = ['a.csv','b.csv','c.csv'], and I want each of my data frames to take the name of the file (a, b,c), I can't do this because the left side of the assignment statement is treated as a string. How do I correct this so that it is interpreted as a dataframe assignment?

mylist = ['a.csv','b.csv','c.csv']
import pandas as pd
for file in mylist:
    file.rsplit('.csv',1)[0] = pd.read_csv(file)

Use a dictionary comprehension:

dfs = {f.rsplit('.csv',1)[0]: pd.read_csv(file)
       for f in mylist}

It is generally considered bad practice to name a variable using a formula. A better solution would be to use a dictionary:

mylist = ['a.csv','b.csv','c.csv']
mydict = {}

import pandas as pd
for file in mylist:
    mydict[file.rsplit('.csv',1)[0]] = pd.read_csv(file)

Once you do this, you can access each dataframe by saying:

mydict['a']
mydict['b']

etc...

I think you can create dictionary of DataFrames :

import pandas as pd

mylist = ['a.csv','b.csv','c.csv']

dfs = {}
for f in mylist:
    dfs.update({f.rsplit('.csv',1)[0]: pd.read_csv(f)})

print dfs['a']  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM